Page 1 of 1

Variable assigned in UDT is NULL in template

Posted: Sat Mar 16, 2019 11:16 pm
by MantaPro
Well, mystified by this one!

I have a udt, lets call it "myudt"

Code: Select all

.......
.......
echo "<pre>Dump1";
var_dump($foo);
echo "</pre>";
$smarty->assignGlobal('foo', $foo);
Then in a template in CMSMS2.2.9.1

Code: Select all

{myudt}
<pre>Dump2
{$foo|@var_dump}
</pre>
<pre>Dump3
{get_template_vars}
</pre>
The resultant displayed data is:
- At Dump1 complete and as expected
- At Dump2 it is null
- At Dump3 it shows up as a variable available with all data complete and correct ???!! (at least to the smarty command {get_template_vars}

(ps. working on a module for use in the CMSMS2x world but the inspiration comes from defunct CMSMS1x module - so I'm guessing now - something different with parsing module templates through Smarty between v1 and v2 ?) - I'll have another read of the module writing tutorial

Re: Variable assigned in UDT is NULL in template

Posted: Sun Mar 17, 2019 10:47 am
by Rolf

Code: Select all

$foo = 'bar';
$smarty->assignGlobal('foo', $foo);

Code: Select all

{myUDT}
{$foo}
bar
This works for me, if not for you in your UDT the value for $foo isn't set before...

Re: Variable assigned in UDT is NULL in template

Posted: Sun Mar 17, 2019 3:23 pm
by MantaPro
Thank you for your reply Rolf

Read it and thought "i know all that and thats isn't going to work either! " - (my bad >:D :-[ ),but tried it for proof (with displays of $foo in the template after the UDT and again in the page after the module's template) and it all worked :. why ? what is different ?

On closer examination of myudt, the issue was I had (because I thought I needed it)

Code: Select all

global $smarty;
$tpl_vars = $smarty->getTemplateVars();
As my first line - and as the last line

Code: Select all

$smarty->assignGlobal("foo", $foo);
everything worked in the UDT and as my original post said in the template after the UDT (or in the page after the module's template) $foo is NULL but $foo is present if I do {get_template_vars}

But your example code prompted me to go back and correct/remove my erroneous first line of code: "global $smarty" that was causing this. Now the variable has data in the module's template and in the page

Many thanks for your help Rolf - all good now