Search code examples
phpsmartytemplate-toolkit

Template Toolkit foreach to Smarty


I have a foreach loop in Template Toolkit that I am converting to Smarty, but I am unsure as to what to change it to.

[% FOREACH ps IN pack_stats %]
<tr>
    [% FOREACH key IN [ id, domain, username, password, plan, price, renew, status ] %]
    <td>[% ps.$key %]</td>
    [% END %]
</tr>
[% END %]

I know that in smarty the foreach loop changes to {foreach from=$pack_stats item=ps}{/foreach} but the text in the center I am unsure as to what to change it to to loop through each of the keys passed to the ps variable.


Solution

  • I don't know if there's a cleaner way to do this, but one thing you can try is assigning an array of keys to a variable and then doing a standard {foreach} over it:

    {assign var='keys' value=','|explode:"id,domain,username,password,..."} 
    {foreach from=$pack_stats item=ps}
      {foreach from=$keys item=key}
        <td>{$ps.$key|escaped}</td>
      {/foreach}
    {/foreach}