Forums / General / variable variables

variable variables

Author Message

Patrick Kaiser

Thursday 30 March 2006 8:33:25 am

Hi,

I would like to use variable variables in a template, and was sure to have done so before, but i can neither find the template nor can i get this running.

I have a class that consists of several elements of the same datatype (object relation), following the naming convention related_[number], where number is an int from 1 to 4.

The following code doesnt work and is only an example. What i want to archive is that [$number] gets substituted by the actual value of $number:

{def $out=array() $myobj=$node.object.data_map}
{for 0 to 4 as $number}
  {set $out = $out|append( hash( 'link', $myobj.related_[$number].content.main_node.url_alias,
'maincat', $myobj.related_[$number].content.main_node.path.1.name,
'teaser', $myobj.related_[$number].main_node.data_map.teaser.content ))}
{/for}

Any ideas would be highly appreciated!

Thanks,

Patrick


Best regards,

Patrick

J Jones

Tuesday 07 August 2007 8:35:20 am

This is an old post I know. And I don't even have a solution.

I need to change the value for a dynamic variable being used in a foreach-loop.

So that I can access i.e. $myvariable later on the page. But there seems to be no option to do this with variables in EZ.

It even surprises me more that I seem to be one of the few who would need this?

Jesper

Patrick Kaiser

Tuesday 07 August 2007 12:32:37 pm

At least for my original problem I found the solution in between.
Given the case you want to access attributes that follow a certain naming convention (ie. attribute_1, attribute_2, attribute_3, etc) you could do the following:

{def $attribute_pre='attribute_' $tmp=''}
{for 0 to 4 as $number}
  {set $tmp=concat( $attribute_pre, $number )}
  {$node.data_map[$tmp].content}
{/for}


Best regards,

Patrick