Page 1 of 1

Cannot use $this in a template

Posted: Mon Nov 18, 2013 6:32 am
by ronshe
Hi I am CmsMs new comer, but experienced in smarty.
I bumped into a strange behavior in a template.
It does not recognizes $this as a smarty pointer and crashes when I try to use it for assign.

Code: Select all

{php}
$solutions = get_solutions_from_db();
$this->assign('solutions', $solutions);   // CRASHES
{/php}

{$solutions|@print_r}
I found out that there is a $smarty pointer. It does not crash when I use assign with it, but the variable is not set either.

Code: Select all

{php}
$solutions = get_solutions_from_db();
global $smarty;
$smarty->assign('solutions', $solutions);   // OK
{/php}

{$solutions|@print_r}     {* NOTHING *}
Please help,
Ronny

Re: Cannot use $this in a template

Posted: Mon Nov 18, 2013 9:17 am
by velden
Use a UDT for php code.

IIRC {php} tag is nog supported (anymore) in cmsms

Re: Cannot use $this in a template

Posted: Mon Nov 18, 2013 11:14 am
by ronshe
It is an existing website with many {php} tags.
I will not convert them all.
Is there any simple solution to my problem?

Re: Cannot use $this in a template

Posted: Mon Nov 18, 2013 12:36 pm
by Jo Morg
velden wrote:IIRC {php} tag is nog supported (anymore) in cmsms
Not only that: http://www.smarty.net/docs/en/language.function.php.tpl
IMPORTANT NOTICE

{php} tags are deprecated from Smarty, and should not be used. Put your PHP logic in PHP scripts or plugin functions instead.
Note

As of Smarty 3.1 the {php} tags are only available from SmartyBC.
You'll have problems upgrading either CMSMS or Smarty anytime in the future.
Besides, it was always considered bad practice to mix logic and design which led to Smarty abandoning that function.
Having said that, I didn't tested it my self but I presume that you would need to get the Smarty object on top of every template. On the 1st {php} block of every template try:

Code: Select all

{php}
$smarty = cmsms()->GetSmarty();
.....
.... 
Nevertheless, converting all {php} blocks to UDT's should be in the horizon. Any upgrade to either CMSMS or Smarty (or both as a CMSMS release may have the most recent Smarty lib tied to it) may easily break the site.
HTH

Re: Cannot use $this in a template

Posted: Tue Nov 19, 2013 2:08 pm
by ronshe
Thanks Jo Morg,

It did not work here. It seems like I will have to workaround this problem.

Ronny.