在小胡子里,有没有办法检查清单中是否有物品,但不能重复吗?
问题描述:
我希望能够检查列表是否为空,以及是否不打印块,但我不想为每个项目重复该块。我只是想能够回应一次。在小胡子里,有没有办法检查清单中是否有物品,但不能重复吗?
给这个结构如下:
array(
"teasers" => array(
array("title"=>"Teaser Title 1"),
array("title"=>"Teaser Title 2")
)
);
{{# teasers }}
<div class="items-wrap">
<div class="items">
{{# . }}
<div class="item">
{{ title }}
</div>
{{/ . }}
</div>
</div>
{{/ teasers }}
我想物品,包裹 div来只能打印一次,重复项目格数组中的每个项目。就像现在,items-wrap为teasers数组中的每个项目重复一次。所以...有没有办法检查主阵列是否为空,但不能重复它?
目标是只打印项目 - 包装一次,如果需要的话。
答
是的,有一种方法。小胡子的方法为lenght
。如果等于零,则为false,并且该块不会被渲染。你的榜样:
{{# teasers.length }}
<div class="items-wrap">
<div class="items">
{{# teasers }}
<div class="item">
{{ title }}
</div>
{{/ teasers }}
</div>
</div>
{{/ teasers.length }}
标签{{teasers.length}}
将检查内部{{teasers}}
项目的数量,只会如果不为空渲染块。
更多信息here。
这个答案为时已晚,但我希望它能帮助别人。