This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit ezplatform.com

eZ Community » Forums » Developer » Variable substitution in template vars
expandshrink

Variable substitution in template vars

Variable substitution in template vars

Tuesday 14 February 2012 1:32:29 pm - 7 replies

Hi,

I wonder if there is such thing as variable substitution with template vars, as in PHP:

 $a = 'test';
echo $$a; // would echo the value of a var $test

Background: I have some fields in a content class like field1, field2, field3 etc. and I would like to get their value in a loop, to avoid code repetition:

I'm afraid there is no way to accomplish this, just wanted to confirm...

{foreach...}
{$field$n}
{/foreach}

Cheers,

frank

Tuesday 14 February 2012 9:51:45 pm

Hi Frank,

If I understood you correctly, you can use something like this:

{foreach $object.data_map as $attribute}
    {$attribute.content}
{/foreach}

If this is not your usecase, can you please elaborate more?

Wednesday 15 February 2012 10:06:17 am

Hi Edi,

thanks for taking time to answer. Your example is the standard way of looping through a class' attributes. I have a class with a lot of attributes, some of them I want treated differently. Say I have name1, address1, phone1, name2, address2, phone2 and so on. What I wanted to do is exclude them from the loop and give them a special treatment like this:

{for 1 to 3 as $counter}
Value of names: {$object.data_map.name$counter}
Value of address: {$object.data_map.address$counter}
{/for}

Never mind, it was not a long list, so I built it manually (I needed a html table), but I was wondering if this would be possible, from a template engine point of view. I suppose not...

Regards,

frank

Modified on Wednesday 15 February 2012 10:08:54 am by frank b

Wednesday 15 February 2012 10:18:22 am

You can use something like this:

{for 1 to 3 as $counter}
    Value of names: {$object.data_map[concat( 'name', $counter )]}
    Value of address: {$object.data_map[concat( 'address', $counter )]}
{/for}

Modified on Wednesday 15 February 2012 10:18:45 am by Edi Modrić

Wednesday 15 February 2012 10:56:24 am

Hi Edi,

thanks for the trick with the square bracket notation! It works for what I was trying to achieve. 

Have a nice day,

frank

Wednesday 15 February 2012 11:11:25 am

Hi Edi,

thanks for the trick with the square bracket notation! It works for what I was trying to achieve. 

Have a nice day,

frank

Glad to be of service happy.gif Emoticon

Wednesday 15 February 2012 11:11:59 am

To just exclude a list of attributes from a template you could use something like this:

{def $exclude=array( "name1", "name2")}
{foreach $node.data_map as $attribute}
    {if $exclude|contains($node.data_map.attribute.contentclass_attribute_identifier)}
        {continue}
    {/if}
{/foreach}

If you want to process some attributes differently, you could use something like this:

{foreach $node.data_map as $attribute}
               {switch match=$attribute.contentclass_attribute_identifier}
               {case in=array( 'name1', 'address1' )}
                     Do something special...
               {/case}
               {case match='description'}
                     Do something else....
               {/case}
               {case}
                        Do the default
               {/case}
               {/switch}
{/foreach}

Wednesday 15 February 2012 11:31:32 am

Hi Steven,

thank you, too. I already came up with the solution for attribute exclusion from your first example. The second one looks good, too.

Thanks for your time,

frank

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from