Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 184 Vote(s) - 3.37 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a smarty variable in prestashop 1.5

#1
I am working on a button that switches view with onClick. I wish to store the last/default position in a variable in order to prevent switching to the default view state on each page refresh or navigation.

I read that I can do the following in a php file:

$myVar= -1;
$smarty->assign('myVar', $myVar);

and then use `$myVar` in the tpl file. But it does not work for me.
The tpl file I am working on is not part of a module and has no .php file in the prestashop root folder.

Can anyone educate me a little on smarty/php and how to create variables and use them to store button's state?

Thanks
Reply

#2
In order to use variables in your smarty file, you need to use for example :

$this->context->smarty->assign(
array(
'myVar' => $myvar,
'otherVar' => $otherVar
)
);

Then to use it in your tpl file you simply need to use :

<div>my var = {$myVar}</div>

To use a variable in your smarty you need to write it inside {}.
Reply

#3
Smarty is a PHP template engine for PHP, which facilitates the separation of presentation (XHTML/CSS) from the PrestaShop's core functions/controllers.

A template file (usually with a .tpl extension in PrestaShop) is always called by a PHP controller file (it can be a Front-end core controller or a module controller).

Example: `/prestashop/controllers/front/ContactController.php`

$this->context->smarty->assign(array(
'contacts' => Contact::getContacts($this->context->language->id),
'message' => html_entity_decode(Tools::getValue('message'))
));

$this->setTemplate(_PS_THEME_DIR_.'contact-form.tpl');

We can see that this file is retrieving information from the database and assigning it to Smarty.

Then, the 'contact-form.tpl' template will display it to the visitors.

The syntax is pretty similar for modules,
example:`/prestashop/modules/blocklink/blocklink.php`

public function hookLeftColumn($params)
{
$this->smarty->assign('blocklink_links', $this->getLinks());

return $this->display(__FILE__, 'blocklink.tpl');
}

Also, to store values in Smarty variables, you can use the 'assign' function in two ways:

1. `$this->context->smarty->assign('my_smarty_variable_name', $my_value);`

or if you have several variables:

2. `$this->context->smarty->assign(array('my_smarty_variable_name1' => $my_value1), ('my_smarty_variable_name2' => $my_value2));`

And then in the Smarty template:

The value of my variable is {$my_smarty_variable_name|escape:'htmlall':'UTF-8'}.

The 'escape' modifier is used to avoid XSS security issues.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through