smarty template variable inside a variable from function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

smarty template variable inside a variable from function

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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).
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Dude, you got me :? . Wish I could help, but this one is out of my realm of expertise.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

perfect, this is exactly what I was looking for:

Code: Select all

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