Page 1 of 1

smarty template variable inside a variable from function

Posted: Wed Jul 05, 2006 2:05 pm
by Burrito
how can I set a value to a variable inside a smarty template that is based on the return value of another function.

ex:

Code: Select all

{my_image srcsession=$image->image extra='width="75"' isth='get_elementlink type="Downloadable File" element=$image'}
obviously that just sets the param value of isth to the string 'get_elementlink type=...'.

what I need that to do is actually call the smarty function 'smarty_function_get_elementlink()' and use its returned value for that param value.

Posted: Wed Jul 05, 2006 2:18 pm
by RobertGonzalez
I think you can call the function by using the function name directly, can't you. I have done this, accidentally, in TemplateLite, which is based on the Smarty codebase.

Posted: Wed Jul 05, 2006 3:10 pm
by Burrito
but I need to return a value from the function based on parameters I send to it (this is the second function mind you (get_elementlink)). With that value, I need to pass it to my primary function (the main one I'm calling).

Posted: Wed Jul 05, 2006 3:30 pm
by RobertGonzalez
Dude, you got me :? . Wish I could help, but this one is out of my realm of expertise.

Posted: Wed Jul 05, 2006 3:38 pm
by Burrito
essentially I just need to assign a variable using another variable but at runtime of the .tpl.

ex in php code:

Code: Select all

$myVar = someFunction($someParam1,$someParam2);
$mySecondVar = someOtherFunction($myVar);
but with smarty it's a little tougher, I know I need to use the assign function, just not sure how to have the 'value' parameter be dynamically assigned:

ex:

Code: Select all

<html>
<body>
blah blah
{assign var='somevariable' value='call_to_function param1="someparam" param2="someotherparam"'}
</body>
</html>
as you might guess, that just sets the value of my runtime var to that string. I've tried plugging in an eval in there too, same result grrr!

Posted: Wed Jul 05, 2006 4:08 pm
by RobertGonzalez
Can that actually be done template-side? I didn't know smarty could do that. I have never seen anything like that in the docs.

Posted: Wed Jul 05, 2006 4:22 pm
by Burrito
you can definitely assign variables at runtime using {assign var="myvar" value="myvalue"} and you can definitely call functions using {myneatfunction param="somevalue" param2="some other value"}.

I don't see why you shouldn't be able to put the two together...I just don't know how.

Posted: Wed Jul 05, 2006 10:33 pm
by RobertGonzalez
When you call the function, can you read it into a var? If you can, call the function and read it into a var, then read that var into the other var.
----------
That didn't really even make sense to me, but I had to throw it out there. That little voice in my head was taunting me to 'Be a man, do it!'. So I did it.

Posted: Thu Jul 06, 2006 5:09 am
by jmut
Maybe you look for somthing like this?

Code: Select all

function smarty_function_DoSomeStuff($params,& $smarty)
{

    $param1 = $params['param1'];
    $param2 = $params['param2'];

    //...some logic  using $param1, $param2 and whatever
    $return = 'something';


    if (empty($params['assign'])) {
        return $return;
    } else {
        $smarty->assign($params['assign'],$return);
    }

}
then in smary you could do

Code: Select all

//returnes result where function is called
{DoSomeStuff param1='somevariable' param2 = 'someotherparam'} 

//returnes result in a variable for further usage
{DoSomeStuff param1='somevariable' param2 = 'someotherparam' assign='output'} 


...code    using {$output}

Posted: Thu Jul 06, 2006 9:07 am
by Burrito
perfect, this is exactly what I was looking for:

Code: Select all

{DoSomeStuff param1='somevariable' param2 = 'someotherparam' assign='output'}
thanks very much!