【问题标题】:In Mustache, is there a way to check of the list has items, but not repeat it?在 Mustache 中,有没有办法检查列表中有项目,但不重复?
【发布时间】:2017-01-22 12:00:34
【问题描述】:

我希望能够检查一个列表是否为空,如果它不是打印一个块,但我不想为每个项目重复该块。我只想能够回显一次。

给出以下结构:

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 }}

我希望 items-wrap div 只打印一次,并为数组中的每个项目重复 item div。就像现在一样,items-wrap 对 teasers 数组中的每个项目重复一次。那么……有没有办法检查主数组是否为空,但不重复?

如果需要,目标是只打印一次items-wrap

【问题讨论】:

    标签: mustache.php


    【解决方案1】:

    是的,有办法。 Mustache 有方法length。如果等于零,则为 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

    这个答案为时已晚,但我希望它对某人有所帮助。

    【讨论】:

      【解决方案2】:

      在 PHP 方面:

      {{#myArray.0}}...{{/myArray.0}}

      https://stackoverflow.com/a/23786928/5546916

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-23
        • 1970-01-01
        • 2017-06-23
        • 2017-01-20
        • 1970-01-01
        • 2021-01-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多