Page 1 of 1

Retrieve custom assigned template variable in a UDT [SOLVED]

Posted: Sat Jun 19, 2010 6:51 pm
by antosha
Hi,
I want to share a little problem that I faced today while writing a UDT that would perform a str_replace.
This is my cms template :

Code: Select all

...
{capture assign=mycustomvariable}{content block="Fruits" wysiwyg="false"}{/capture}
{mycustomudt}
...
and this is the UDT mycustomudt :

Code: Select all

global $gCms;
$healthy = array("apple", "orange", "pear");
$yummy   = array("pizza", "beer", "ice cream");

$yummyoutput = str_replace($healthy, $yummy, $mycustomvariable);
echo $yummyoutput ;
Now, when I look at my page in a browser, nothing shows up.
I suspect that for some reason the UDT doesn't retrieve $mycustomvariable...
What am I doing wrong?

Thanks :)

Re: Retrieve custom assigned template variable in a UDT

Posted: Sun Jun 20, 2010 12:35 am
by calguy1000
use this:

Code: Select all

$mycustomvariable = $smarty->get_template_vars('my_custom_vars');

Re: Retrieve custom assigned template variable in a UDT

Posted: Sun Jun 20, 2010 2:35 pm
by NaN
Or try this:

Code: Select all


...
{capture assign=mycustomvariable}{content block="Fruits" wysiwyg="false"}{/capture}
{mycustomudt var="$mycustomvariable"}
...

Code: Select all


$mycustomvariable = '';
if(isset($params['mycustomvariable']))
         $mycustomvariable = $params['mycustomvariable'];

$healthy = array("apple", "orange", "pear");
$yummy   = array("pizza", "beer", "ice cream");

$yummyoutput = str_replace($healthy, $yummy, $mycustomvariable);
echo $yummyoutput;

You can pass any param to udts or plugins named whatever you want: {udt foo=bar}
All these params are stored in an array named $params indexed with their param names.
So within the udt or plugin you can access that param via $params['foo']. (wich will return 'bar')

Re: Retrieve custom assigned template variable in a UDT

Posted: Sun Jun 20, 2010 7:51 pm
by antosha
Thanks for your replies,
I tried both solutions and still no luck, the the block on the page shows empty. Could this happen beacause I am still running CMSMS 1.6.7?

Re: Retrieve custom assigned template variable in a UDT

Posted: Sun Jun 20, 2010 8:34 pm
by NaN
Just take a closer look at the code.
There is a small error.
In the template i used var="mycustomvariable".
But in the code i refer to $params['mycustomvariable'] which is indeed wrong.
Regarding to my last explanations (foo=bar => $params['foo'])
It should be $params['var'].

Re: Retrieve custom assigned template variable in a UDT

Posted: Sun Jun 20, 2010 8:57 pm
by antosha
I should have seen this...
Thanks a lot, it works great now !