【问题标题】:Handlebars get last item indexHandlebars 获取最后一项索引
【发布时间】:2017-03-03 11:58:25
【问题描述】:

使用handlebars 生成css3幻灯片。

模板:

{{#slider}}
    <ul>
    {{#each images}}
    <li><img src="{{url}}"" alt="{{caption}}" /></li>
    {{/each}}
    </ul>

    {{#if infinity}}
    <label class="goto-first" for="slides_1"></label>

    {{#foreach images}}
    <label class="goto-last" for="slides_{{#if @last}}{{@index}}{{/if}}"></label>
    {{/each}}

    {/if}}
{{/slider}}

假设我有 4 张图片,第一张没问题,我不碰它,最后需要返回这个 label.goto-last[for="slides_4]

foreach 试过,但我犯了错误。

infinity 是一个滑块选项false/true

有什么想法吗?谢谢。

【问题讨论】:

    标签: javascript jquery handlebars.js


    【解决方案1】:

    如果您使用的是 json 'array',如果当前索引等于数组长度,那么它将是最后一项。

    How to find Array length inside the Handlebar templates?

    【讨论】:

    • 谢谢。是的,我正在使用 json 数组和 images.length 这成功了
    • 实际上这里有一个问题.. 对于 3 张图像我有 @index 返回 0,1,2 和 images.length 返回 3。我怎样才能得到 2 而不是 3?像长度-1
    • 找到了解决方案,使用 {{last images}} 和 Handlebars.registerHelper("last", function(array) {return array.length-1;});
    猜你喜欢
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 2011-12-14
    相关资源
    最近更新 更多