【问题标题】:Split Smarty looped array in half将 Smarty 循环阵列分成两半
【发布时间】:2014-01-24 16:09:57
【问题描述】:

我在控制器中设置了一个简单的数组用于测试$this->view_data['test'] = array(1,2,3,4,5,6,7,8,9,10); 我想使用 Smarty3 输出数组,以在一个列中显示前半部分,在下一列中显示后半部分。

<div class="col-md-6">
    <!-- first half -->
    {foreach $test as $t}
        {$t}
    {/foreach}
</div>
<div class="col-md-6">
    <!-- second half -->
    {foreach $test as $t}
        {$t}
    {/foreach}
</div>

【问题讨论】:

    标签: smarty smarty3


    【解决方案1】:

    您可以创建一个额外的变量来存储除以 2 的数组长度。在 smarty 3.1 中测试

    {{assign var="cnt" value=$test|@count}} 
    {{math equation="ceil(cnt/2)" cnt=$test|@count assign=dv}}
    <div class="col-md-6">
    {{section name=i loop=$test}}   
        {{$test[i]}}                    
    {{if $smarty.section.i.iteration % $dv == 0 AND $smarty.section.i.iteration-1 < $cnt}}
    </div><div class="col-md-6">
    {{/if}}
    {{/section}}
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 2020-09-21
      • 2013-05-25
      • 2013-12-06
      • 2012-05-26
      相关资源
      最近更新 更多