简体   繁体   English

检查每个助手中的项目是否最后

[英]Checking if item is last in each helper

I have a set of items and I want to make a row element around every 4 items, I found this helper here 我有一组项目,我想每隔4个项目创建一个row元素,我在这里找到了这个帮助器

https://funkjedi.com/technology/412-every-nth-item-in-handlebars/ https://funkjedi.com/technology/412-every-nth-item-in-handlebars/

Handlebars.registerHelper('grouped_each', function(every, context, options) {
    var out = "", subcontext = [], i;
    if (context && context.length > 0) {
        for (i = 0; i < context.length; i++) {
            if (i > 0 && i % every === 0) {
                out += options.fn(subcontext);
                subcontext = [];
            }
            subcontext.push(context[i]);
        }
        out += options.fn(subcontext);
    }
    return out;
});

Which accompanied with the each helper I can do this 在每个助手的陪同下,我可以做到这一点

{{#grouped_each 4 data}}
<div class="message-row">
  {{#each this}}
    <div class="message">
      [Content here...]
    </div>
  {{/each}}
</div>
{{/grouped_each}}

Which works well, every 4 items I get a new row element, but the last row will always be one element short, because in the last elements place I want to play a generic "Read more" type of item. 效果很好,每4个项目我都会得到一个新的row元素,但最后一行将总是一个元素短缺,因为在最后一个元素位置,我想播放通用的“阅读更多”类型的项目。

Ideally I would just like something like this 理想情况下,我只想要这样的东西

{{#grouped_each 4 data}}
<div class="message-row">
  {{#each this}}
    <div class="message">
      [Content here...]
    </div>
    {{#if $last}}
      <div class="message">
        [Content for last item here...]
      </div>
    {{/if}}
  {{/each}}
</div>
{{/grouped_each}}

Where the helper can just make that isLast variable true when it's on the final item, and thus render the final read more item after the final item has been rendered. 当助手将isLast变量放在最终项目上时,它可以使该变量为true,从而在渲染最终项目后渲染最终的read more项目。 I'm having trouble understanding how I can pass a variable to the helper that tells me that. 我在理解如何将变量传递给告诉我的帮助程序时遇到了麻烦。

I've seen this work before but I can't seem to tweak the snippets I've seen to work for my scenario. 我以前看过这项工作,但似乎无法调整我所见过的片段适用于我的情况。

I think what you want to do, if indeed you want to do it this way, is to append a "last: true" key/value to your subcontext that is output by your helper, instead of trying to pass something into the helper, so that the template gets access to it. 我认为您想要做的是,如果确实要这样做,是在助手输出的子上下文中附加一个“ last:true”键/值,而不是尝试将某些内容传递给助手,以便模板可以访问它。 This may require re-shaping the data structure of your subcontext a bit. 这可能需要重新调整子上下文的数据结构。 But then you would be able to check 但是这样您就可以检查

{{#if this.last}}
    <somestuff></somestuff>
{{/if}}

Here's a quick shot at how you might do that using your own code from above: 下面是您如何使用上面的代码来实现此目的的简要概述:

Handlebars.registerHelper('grouped_each', function(every, context, options) {
var out = "", subcontext = {}, i;
if (context && context.length > 0) {
    for (i = 0; i < context.length; i++) {
        if (i > 0 && i % every === 0) {
            out += options.fn(subcontext);
            subcontext.list = [];
        }
        if (i === context.length - 1){
            subcontext.last = true;
        }
        subcontext.list.push(context[i]);
    }
    out += options.fn(subcontext);
}
return out;
});

then in your template do: 然后在您的模板中执行以下操作:

{{#grouped_each 4 data}}
<div class="message-row">
  {{#each this.list}}
    <div class="message">
      [Content here...]
    </div>
    {{#if this.last}}
      <div class="message">
        [Content for last item here...]
      </div>
    {{/if}}
  {{/each}}
</div>
{{/grouped_each}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM