如何在聪明中使用foreach增加变量


How to increment variable using foreach in smarty?

这是我赋值的变量

 {assign var="count" value=0}

我将变量放在foreach语句中

{foreach from=$name item=names}
   {assign var="count" value=$count+1}
{/foreach}

想要在名为"friendsCount"的div中显示变量

<div class="friendsCount">
   Total friends:{$count}
</div>

但是当程序错误消息显示"试图获取非对象的属性"时。请帮助我找到解决方案。正在使用编码点火器

当你在 smarty 中声明变量时,你不需要""。你可以简单地这样声明。

{assign var=count value=0}
{foreach from=$name item=names}
    {assign var=count value=$count+1}
{/foreach}

您可以使用{$friends|count}计算数组的总项目数

文档:http://smarty.incutio.com/?page=SmartyFrequentlyAskedQuestions#template-1