[SOLVED] Smarty - variables within variables

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
spcherub
Power Poster
Power Poster
Posts: 402
Joined: Fri Jun 06, 2008 5:54 pm

[SOLVED] Smarty - variables within variables

Post by spcherub »

I've been wrestling with this one issue for a while and am hoping the Smarty-smarties in this forum will be able help.

What I need to be able to do is embed variables within variables within Smarty. Here's the use case:

Assume the following variables have been defined as assigned elsewhere in the template:

Code: Select all

{assign var=track1 value="One"}
{assign var=track2 value="Value value"}
{assign var=track3 value="Some test value"}
... and so on to track16.

I'd like to be able to write a loop that runs through these variables and prints out these values (instead of printing them out one by one manually). So something like (pseudocode)

Code: Select all

{section name=foo start=1 loop=17 step=1}
  {assign var=myvar value="track"$smarty.section.foo.index}
  {eval $myvar}
{/section}
The desired output is:
One
Value value
Some test value
...


but all I get is
track1
track2
track3

...

So question is can I embed variables within variables in smarty and use eval or something else to process it as desired.

Any helpful hints will be much appreciated.

TIA
Sanjay
Last edited by spcherub on Wed Sep 01, 2010 4:35 pm, edited 1 time in total.
Jos
Support Guru
Support Guru
Posts: 4020
Joined: Wed Sep 05, 2007 8:03 pm

Re: Smarty - variables within variables

Post by Jos »

Can't you use an array for this? So var=track[1] in stead of var=track1

http://www.smarty.net/manual/en/languag ... iables.php
JeremyBASS

Re: Smarty - variables within variables

Post by JeremyBASS »

That link may not help.. what you want is


{assign var=myvar value="track`$smarty.section.foo.index`"}  note the back ticks

and never use the eval unless you have a reason as it'll slow your site down...

Cheers -Jeremy
spcherub
Power Poster
Power Poster
Posts: 402
Joined: Fri Jun 06, 2008 5:54 pm

Re: Smarty - variables within variables

Post by spcherub »

Jeremy,

Thanks for the pointers on concatenation with back-ticks. Unfortunately my particular problem is not with concatenation but with being able to get at the value of variable where the variable name inside the value of another variable. So what I need is a way to do {${$myvar}} which is not valid Smarty syntax but gets the point across.

I ended up with the following loop (using your idea):

Code: Select all

{section name=foo start=1 loop=3 step=1}
	{assign var=myvar value="track`$smarty.section.foo.index`"}
	{eval var=$myvar}<br />
{/section}
but the problem still remains that I cannot get at the value of $track1 or $track2.

I also tried:

Code: Select all

{assign var=myvar value="{$track`$smarty.section.foo.index`}"}
based on Jeff's suggestion but that did not work as Smarty was not able to parse the value properly.

I am using the eval because I'm assuming it will interpret/expand the embedded tags.

Thanks,
Sanjay
spcherub
Power Poster
Power Poster
Posts: 402
Joined: Fri Jun 06, 2008 5:54 pm

Re: Smarty - variables within variables

Post by spcherub »

Here is a full test case in case someone wants to try this out themselves.

Code: Select all

{assign var=track1 value="This is the first track"}
{assign var=track2 value="This is the second track"}

{section name=foo start=1 loop=3 step=1}
	{assign var=myvar value="track`$smarty.section.foo.index`"}
	Output: {eval var=$myvar}<br />
{/section}
What I would like to see is the following output:
Output: This is the first track
Output: This is the second track


Thanks,
Sanjay
spcherub
Power Poster
Power Poster
Posts: 402
Joined: Fri Jun 06, 2008 5:54 pm

Re: [SOLVED] Smarty - variables within variables

Post by spcherub »

Thank to Jeff Bosch (ajprog), this has been solved and here is the solution. The key was to find a way to embed the { and } into the variable value and to do an eval so Smarty would interpret the variable within a variable. Using {rdelim} and {ldelim} is a neat way around the smarty syntax. Yes it is a little roundabout (3 assignments to get one value), but the solution works for when there are 16 variables I need to look for like in my case.

Code: Select all

{section name=foo start=1 loop=3 step=1}
	{capture assign=myname}{ldelim}$track{$smarty.section.foo.index}{rdelim}{/capture}
	{eval var=$myname assign=trackname}
	{* Here I can use $trackname as I would any regular variable *}
	{if $trackname != ''}Output:{$trackname}{/if}
{/section}

Thanks,
Sanjay
JeremyBASS

Re: [SOLVED] Smarty - variables within variables

Post by JeremyBASS »

Ha, my bad, I totally missed your point there.. sad as I even have a blog on this same trick lol.. Sorry to waste you time there  :-\
spcherub
Power Poster
Power Poster
Posts: 402
Joined: Fri Jun 06, 2008 5:54 pm

Re: [SOLVED] Smarty - variables within variables

Post by spcherub »

No problem at all - you did give me good hints on concatenation using back-ticks. Also the discussion was what helped find the final solution.

Go Forum!

S
Last edited by spcherub on Thu Sep 02, 2010 12:41 pm, edited 1 time in total.
Post Reply

Return to “The Lounge”