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:
1 2 3 4 5 6 7 |
public function hookDisplayOrderConfirmation($params) { $this->context->smarty->assign(array( 'message' => $this->l('Thank you for your purchase!') )); return $this->display(__FILE__, 'order-confirmation.tpl'); } |
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:
1 |
<div id="block-order-detail">{$message}</div> |
Once the template rendering is over, we get the following result:
1 |
<div id="block-order-detail">Thank you for your purchase!</div> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class OrderConfirmationControllerCore extends FrontController { public function initContent() { if (Configuration::isCatalogMode()) { Tools::redirect('index.php'); } parent::initContent(); $order = new Order(Order::getOrderByCartId((int)($this->id_cart))); $presentedOrder = $this->order_presenter->present($order); $register_form = $this->makeCustomerForm()->setGuestAllowed(false) ->fillWith(Tools::getAllValues()); $this->context->smarty->assign(array( 'HOOK_ORDER_CONFIRMATION' => $this->displayOrderConfirmation($order), 'HOOK_PAYMENT_RETURN' => $this->displayPaymentReturn($order), 'order' => $presentedOrder, 'register_form' => $register_form, )); if ($this->context->customer->is_guest) { /* If guest we clear the cookie for security reason */ $this->context->customer->mylogout(); } $this->setTemplate('checkout/order-confirmation'); } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
public function renderForm() { $product_field_tpl = $this->context->smarty->createTemplate( _PS_MODULE_DIR_ . 'belvg_subscriptions/views/templates/front/product_field.tpl' ); $product_field_tpl->assign(array( 'products' => $products, )); $fields_form = array( 'form' => array( 'legend' => array( 'title' => $this->trans('Settings', array(), 'Admin.Global'), 'icon' => 'icon-cogs' ), 'input' => array( array( 'label' => $this->trans('Products:'), 'type' => 'html', 'name' => 'products', 'html_content' => $product_field_tpl->fetch(), ), ), 'submit' => array( 'title' => $this->trans('Save', array(), 'Admin.Actions') ) ), ); $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT')); $helper = new HelperForm(); $helper->show_toolbar = false; $helper->table = $this->table; $helper->default_form_language = $lang->id; $helper->module = $this; $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0; $helper->identifier = $this->identifier; $helper->submit_action = 'submitStoreConf'; $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->tpl_vars = array( 'uri' => $this->getPathUri(), 'fields_value' => $this->getConfigFieldsValues(), 'languages' => $this->context->controller->getLanguages(), 'id_language' => $this->context->language->id ); return $helper->generateForm(array($fields_form)); } |
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.
May i have source ?