简体   繁体   中英

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.

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}
{$keys = ["id", "domain", "username", "password", "plan", "price", "renew", "status"]}
{foreach $pack_stats as $ps}
  <tr>
    {foreach $keys as $k}
      <td>{$ps.$k|escape}</td>
    {/foreach}
  </tr>
{/foreach}

(Smarty3 syntax)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM