Prestashop 1.6 – 1.7 MVC. Part 3: Creating a View

Prestashop 1.6 – 1.7 MVC. Part 3: Creating a View

In the previous part of the MVC in Prestashop CMS article we focused on creating a Controller. In this part we will describe the process of creating a View.

To create a View Prestashop uses the Smarty template engine. Smarty is a PHP template compiling handler. To speak precisely, it is a tool which helps us to separate the applied logic and data from the view. It is quite convenient to use when there are different people responsible for the development and for the template makeup.

The smarty syntax is quite simple and should not cause any difficulties, you may find more details following the above link. So, where should we use smarty when creating a view in Prestashop CMS?

1. Hooks:

Since hooks are implemented in modules, the template file should be located under the following path: /modules/name_module/views/templates/hook/order-confirmation.tpl

order-confirmation.tpl:

Once the template rendering is over, we get the following result:

We can transmit not only text or numeric variables to our template, but also arrays and objects and even use them in cyclic expressions (foreach, for). Modules’ view files can be overridden under the following path: /themes/theme_name/modules/module_name/path_to_template_us_in_module

2. Controllers:

In this example we add the variables HOOK_ORDER_CONFIRMATION, HOOK_PAYMENT_RETURN, order, register_form to the order-confirmation.tpl template with the help of a smarty object and the assign() method. The view file is located here: /themes/theme_name/templates/checkout/order-confirmation.tpl.

The difference between a controller and a module view is that module’s display() method instantly returns the html code of the view while for controller this happens only after the setTemplate() method is implemented.

There is one more method you can use to create a view, but I use it generally when making fields for various forms because HelperForm does not always suit my needs.
Example:

With this article we finish discussing the topic of MVC in Prestashop CMS.  In this last part we have described how to create a Module’s Controller and View.



1 comment

Post a new comment

top