Reputation: 2614
I currently have an issue with setting a cookie in a block of php code that is in a .tpl smarty template class.
In the .tpl class I have somethng like :
{php}
setcookie("school", "test");
{/php}
School: { $smarty.cookies.school }
However this does not print out anything. I know the php setcookie call works when its in a .php file alone, since I've tested that, but can't get it to work with the php snippet of code is in a .tpl class.
Any Advice Appreciated, D
Upvotes: 1
Views: 9974
Reputation: 2614
Instead of putting embedded php code into my template, I created a smarty custom function plugin that when called allows me to set cookies.
Details about custom functions: http://www.smarty.net/docsv2/en/plugins.functions.tpl Peter pointed me to the idea of doing this.
However, I'm still a little curious on how this can be done on the embedded php in the template code even though it isn't suggested as good practice.
Upvotes: 1
Reputation: 16943
{php}
setcookie("school", "test");
$_COOKIE['school'] = 'test';
{/php}
It's because $_COOKIE superglobal is updated only on page refresh, not after using setcookie()
Make sure you are using SmartyBC.class.php
not Smarty.class.php
because {php}{/php}
will not work using Smarty.class.php.
Also remember to set $php_handling
property to SMARTY_PHP_ALLOW.
More info http://www.smarty.net/docsv2/en/variable.php.handling.tpl
Upvotes: 5