views:

41

answers:

1

Hi. I have a simple question. I need to append 1,2,3 etc to a variable name in smarty. The variables are $user1, $user2, $user3 etc. I use smarty to do a simple loop like this:

     {section name=user_info start=1 loop=$users}
       <tr>
        <td> User{$smarty.section.user_info.index} </td> // prints user1, user2, user3 etc

        <td>
         {$user} append this: {$smarty.section.user_info.index} // need to call $userX
        </td>

      </tr> 
    {/section} 

I tried everything on the variable info site to smarty.

+2  A: 

Make $user a numerically index array instead of declaring a variable for each user as you have now ($user1, $user2, $user3) and then you can use:

{$foo.$bar} <-- display variable key value of an array, similar to PHP $foo[$bar]

According to Smarty Variables manual

Once converted this would be like:

$user = array(
    0 => $user0,
    1 => $user1,
);

So in your case it would be {$user.$smarty.section.user_info.index}.

Treffynnon
And also in section you can access data from arrayThe name of the {section} can be anything you like, see PHP variables. It is used to reference the data within the {section}.{section name=anything loop=$myArray} {$myArray[anything].foo} {$name[anything]} {$address[anything].bar}{/section}
Tomasz Struczyński