【问题标题】:smarty add a counter and check the values and add classsmarty 添加一个计数器并检查值并添加类
【发布时间】:2014-06-03 12:13:09
【问题描述】:

在 smarty 中,我想设置一个计数器值并检查它的增量。在简单的php中代码是这样的

$i = 0;
while ( $someValues ) :
$i++;
<div class="test ''.$class.">$number</div>
endwhile;

现在在 php 中我想添加这样的类

if($i==1) {
    $class= 'active';
  }
  else {
    $class= '';
  }

在 smarty 中,我将数据库中的所有值都分配给变量 someValues。 所以在这里我会像这样检查

{foreach from=$someValues item=row}
<div class="test">$row.number</div>
{foreach}

这里给出这样的结果

<div class="test">1</div>
<div class="test">2</div>
<div class="test">3</div>
<div class="test">4</div>
<div class="test">5</div>

现在我想要相同类型的计数器并想添加类。那么有人可以告诉我该怎么做吗? 我曾尝试使用 while 循环进行检查,但它显示总页为空白。

【问题讨论】:

    标签: php smarty prestashop


    【解决方案1】:

    从 1 开始的索引

    {$smarty.foreach.name.iteration}
    

    如果你想在foreach的第一个div中添加class,你可以简单地使用

    <div class="test{if $smarty.foreach.name.first} first{/if}">text</div>
    or
    <div class="test{if $smarty.foreach.name.index == 0} first{/if}">text</div>
    or
    <div class="test{if $smarty.foreach.name.iteration == 1} first{/if}">text</div>
    

    编辑: 更多信息:http://www.smarty.net/docsv2/en/language.function.foreach.tpl

    【讨论】:

      猜你喜欢
      • 2016-10-18
      • 2014-11-09
      • 2019-05-22
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多