Cannot use $this in a template

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
ronshe
New Member
New Member
Posts: 3
Joined: Mon Nov 18, 2013 6:12 am

Cannot use $this in a template

Post 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
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Cannot use $this in a template

Post by velden »

Use a UDT for php code.

IIRC {php} tag is nog supported (anymore) in cmsms
ronshe
New Member
New Member
Posts: 3
Joined: Mon Nov 18, 2013 6:12 am

Re: Cannot use $this in a template

Post 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?
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1974
Joined: Mon Jan 29, 2007 4:47 pm

Re: Cannot use $this in a template

Post 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
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
ronshe
New Member
New Member
Posts: 3
Joined: Mon Nov 18, 2013 6:12 am

Re: Cannot use $this in a template

Post by ronshe »

Thanks Jo Morg,

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

Ronny.
Post Reply

Return to “The Lounge”