0

我无法更改传递给 Smarty 的数组,尽管目前我有:

{if $cms.featured && $cms.featured.haspictures}
{foreach from=$cms.featured.pictures key=id item=row name=foo}
{if $id eq 0}
<div class="sliderkit-panel">
    <a href="{$cms.featured.url}" title="{$cms.featured.title|truncate:60}"><img src="{$HOME}/get/wide{$row.filename}" alt="{if $row.description}{$row.description}{else}{$cms.featured.title}{/if}" /></a>
</div>
{/if}
{/foreach}
{/if}

{if $cms.featured1 && $cms.featured1.haspictures}
{foreach from=$cms.featured1.pictures key=id item=row name=foo}
{if $id eq 0}
<div class="sliderkit-panel">
    <a href="{$cms.featured1.url}" title="{$cms.featured1.title|truncate:60}"><img src="{$HOME}/get/wide{$row.filename}" alt="{if $row.description}{$row.description}{else}{$cms.featured1.title}{/if}" /></a>
</div>
{/if}
{/foreach}
{/if}

{if $cms.featured2 && $cms.featured2.haspictures}
{foreach from=$cms.featured2.pictures key=id item=row name=foo}
{if $id eq 0}
<div class="sliderkit-panel">
    <a href="{$cms.featured2.url}" title="{$cms.featured2.title|truncate:60}"><img src="{$HOME}/get/wide{$row.filename}" alt="{if $row.description}{$row.description}{else}{$cms.featured2.title}{/if}" /></a>
</div>
{/if}
{/foreach}
{/if}

无论如何我可以减少这个重复的代码吗?

4

1 回答 1

0

尝试将重复的函数包含在 {for} 循环中并使用变量作为数组索引,例如

{for $loop=1 to 3}
  {$featured='featured'}
  {if $loop!=1}{$featured=$featured|cat:$loop}{/if}

  {if $cms.$featured && $cms.$featured.haspictures}
  ....


{/for}
于 2013-02-04T10:09:55.787 回答