Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Variable variables
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
OhReally
Guest





PostPosted: Wed Feb 06, 2008 12:54 pm    Post subject: Variable variables Reply with quote

Searching for a way to use variable variables with Smarty, I only found messages saying they are not supported. So I wrote a plugin to make them supported...

The site I'm developing includes tips. These tips can all have upto 5 links attached. They are called url_1, ..., url_5.

This is what my template looks like (some garbage removed for readability):
Code:

{get_tip tip=$tipid assign="tip"}
{section name="links" start=1 loop=6}
{varvar var="tip.url_`$smarty.section.links.index`"}<br />
{/section}


This is the file function.varvar.php in my plugins dir:
Code:

<?php
function smarty_function_varvar($args, &$smarty) {
    if (empty($args["var"])) {
        return;
    }
    $array = explode(".", $args["var"]);
    $var = array_shift($array);
    $val = $smarty->get_template_vars($var);
    if (count($array) == 0) {
        return $val;
    }
    else {
        $idx = "['" . join("']['", $array) . "']";
        eval("\$return = \$val$idx;");
        return $return;
    }
}
?>


Of course, if you don't use arrays, function.varvar.php could be reduced to:
Code:

function smarty_function_varvar($args, &$smarty) {
    return $smarty->get_template_vars($args["var"]);
}


It may not be the prettiest solution, and it's definitely more code than '$$', but it works.

Thought I'd share it.

Edit: I've added this plugin to the SmartyWiki.


Last edited by OhReally on Thu Feb 07, 2008 5:35 pm; edited 1 time in total
Back to top
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Wed Feb 06, 2008 1:18 pm    Post subject: Reply with quote

Thanks for sharing Smile

Why don't you use an array to store the links instead of multiple variables?
_________________
Darn computers always do what I tell them to instead of what I want them to do.
Back to top
View user's profile Send private message
OhReally
Guest





PostPosted: Wed Feb 06, 2008 4:07 pm    Post subject: Just to prove a point... ;) Reply with quote

Celeb wrote:
Why don't you use an array to store the links instead of multiple variables?


Because MySQL doesn't support arrays, and I assign $tip like
Code:

$result = $db->query("select ...");
$tip = $result->fetch_assoc();
$smarty->assign("tip", $tip);


I could, of course, do things to $tip before I assign it, but that would be a choice between PHP and PHP...

And when over 800 Google hits tell me Smarty doesn't support variable variables, I want to prove it does... Wink
Back to top
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Feb 06, 2008 9:18 pm    Post subject: Reply with quote

I have written a small modifier to do the job.

File modifier.varvar.php

Code:
<?php

function smarty_modifier_varvar($string)
{
    global $smarty;
    return $smarty->_tpl_vars[$string];
}

?>


Template
Code:

       {assign var="ourTest" value="whoo"}
       {assign var="our" value="our"}
       {assign var="test" value="Test"}
       {assign var="finally" value="$our$test"|varvar}

Each of the following will output  "whoo"

{$finally}
{"$our$test"|varvar}
{"our$test"|varvar}
Back to top
View user's profile Send private message
OhReally
Guest





PostPosted: Fri Feb 08, 2008 6:41 pm    Post subject: Reply with quote

When I was writing this function, I was wondering whether it should be a function or a modifier...

Can someone tell me which would be the preferred way? The only real difference I see, is that the modifier needs to declare $smarty global, where the function doesn't because it's passed by reference.

Is there a difference in cost?
Back to top
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Feb 08, 2008 6:58 pm    Post subject: Reply with quote

Modifiers can be used almost anywhere in the template. I don't think that you can use a function at parameters of other Smarty tags. Also you have to pass parameters to the function in a little bit more complicated way.

So I think in this case a modifier is more flexible and easier to use.
Back to top
View user's profile Send private message
Gaskonijn
Smarty Rookie


Joined: 03 Mar 2008
Posts: 6

PostPosted: Mon Mar 03, 2008 8:50 am    Post subject: Reply with quote

Does anyone now how i use it with arrays? So i hava a variable $waarden where all my values of my submited form are in. So when a user is posted back i want to show him all the variables he posted. so in a textfield I do value="{"$waarden.input_$ids[klantviewfilters]"}" wich gives me Array.input_2 But how do i give the value of that field? Embarassed
some more info: $ids is an array i give in my php script with all the id's of the generated fields. klantviewfilters is a section so that gives the current one in the array. The output i want is the variable $waarden.input_2 wich the value test is stored in. But the clossest i can get is Array.input_2 Sad
Back to top
View user's profile Send private message
OhReally
Guest





PostPosted: Mon Mar 03, 2008 9:18 am    Post subject: Reply with quote

If you drop the first dollar sign, it should work. Also, drop the square brackets, and put a dot between '$ids' and 'klantviewfilters; square brackets are not supported (yet). So:
Code:
value="{"waarden.input_$ids.klantviewfilters"|varvar}"

if you use the modifier (the preferred way), or
Code:
value="{varvar var="waarden.input_$ids.klantviewfilters"}"

if you use the function.
Back to top
Gaskonijn
Smarty Rookie


Joined: 03 Mar 2008
Posts: 6

PostPosted: Mon Mar 03, 2008 9:27 am    Post subject: Reply with quote

Still doesn't work Confused and i did a print_r and got Array ( [input_2] => test ) so there is a value at that place :s

so i have the modifier modifier.varvar.php in my plugins directory with in there the code:
Code:

function smarty_modifier_varvar($string)
{
    global $smarty;
    return $smarty->_tpl_vars[$string];
}


and in my tpl file i have
Code:
value="{"waarden.input_$ids.klantviewfilters"|varvar}"


is this all i need? or am i missing something :s sorry for the noob questions but it's the first time i use smarty for a large project
Back to top
View user's profile Send private message
OhReally
Guest





PostPosted: Mon Mar 03, 2008 9:33 am    Post subject: Reply with quote

U.Tews' modifier doesn't support arrays. Save this code as modifier.varvar.php in your plugins directory.
Back to top
Gaskonijn
Smarty Rookie


Joined: 03 Mar 2008
Posts: 6

PostPosted: Mon Mar 03, 2008 9:40 am    Post subject: Reply with quote

OhReally wrote:
U.Tews' modifier doesn't support arrays. Save this code as modifier.varvar.php in your plugins directory.


I copied that code but get the error:
Code:
Fatal error: Call to a member function get_template_vars() on a non-object


So he is not seeing $smarty as an object :s

I thnk the reason he doesn't find smarty is beacause i'm extending smarty from every page and not using it as an object. Is there any way around so i could use this function? Confused
Back to top
View user's profile Send private message
OhReally
Guest





PostPosted: Mon Mar 03, 2008 10:07 am    Post subject: Reply with quote

I guess you mean you have an object that extends the Smarty object. If that's true, then this should work if you replace '$smarty' in the modifier code with the name you gave your object.
Back to top
Gaskonijn
Smarty Rookie


Joined: 03 Mar 2008
Posts: 6

PostPosted: Mon Mar 03, 2008 10:18 am    Post subject: Reply with quote

Hmmm did that but still doesn't work Confused I get no output at all

Just editing the modifier and echo'ing $idx and i get as a result ['input_Array']['klantviewfilters'] wicht is not correct i think Confused the actual vallue of $return is Array['input_Array']['klantviewfilters'] Shouldn't that be Array['input_Array['klantviewfilters']]; ? so it replaces the Array['klantviewfilters'] with the number stored in there and the result of Array['input_2'] is retrieved?
Back to top
View user's profile Send private message
OhReally
Guest





PostPosted: Mon Mar 03, 2008 10:44 am    Post subject: Reply with quote

I don't know. I don't know your code, so I don't know what values you expect...

But you've lost me with the classes extending classes extending classes anyway, so I don't think I can help you any further Crying or Very sad .

I guess you'll have to write your own modifier. Take my code as an example, and try to understand what it does: it takes a string and uses that as the name of a template variable to return, nothing more, nothing less.

Good luck!
Back to top
Gaskonijn
Smarty Rookie


Joined: 03 Mar 2008
Posts: 6

PostPosted: Mon Mar 03, 2008 10:48 am    Post subject: Reply with quote

thx for the help so far. Will try to hack in it some more and get the results i want Laughing
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP