Variable assigned in UDT is NULL in template

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Locked
MantaPro
Forum Members
Forum Members
Posts: 97
Joined: Sun Feb 03, 2008 8:14 pm

Variable assigned in UDT is NULL in template

Post 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
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Location: The Netherlands
Contact:

Re: Variable assigned in UDT is NULL in template

Post 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...
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
MantaPro
Forum Members
Forum Members
Posts: 97
Joined: Sun Feb 03, 2008 8:14 pm

Re: Variable assigned in UDT is NULL in template

Post 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
Locked

Return to “CMSMS Core”