我正在尝试使用自定义助手设置 Handlebars 模板,但我似乎无法让它工作。 block 助手应该能够像 here 那样对我的数据数组进行切片.
当我在不使用模板的情况下调用数组中的每个索引时,它可以正常工作。
这是我的模板
<script id="template_marcas" type="text/x-handlebars-template">
{{#datos_marcas}}
<div class="large-3 small-3 columns marca_item">
<div class="marca_content">
<h3>{{img}}</h3>
<p>{{descp}}</p>
<a href="{{href}}">Ver descuentos</a>
</div>
</div>
{{/datos_marcas}}
</script>
我的数据数组:
var data_marcas = {
datos_marcas: [
{
"img":"alverto",
"descp":"Descripción Marta",
"href":"test"
},
{
"img":"marta",
"descp":"Descripción Marca",
"href":"test"
},
{
"img":"marca",
"descp":"Descripción Marca",
"href":"test"
} //etc.....
]
};
这是我正在尝试使用的助手
Handlebars.registerHelper('slice', function(context, block) {
var ret = "",
offset = parseInt(block.hash.offset) || 0,
limit = parseInt(block.hash.limit) || 5,
i = (offset < context.length) ? offset : 0,
j = ((limit + offset) < context.length) ? (limit + offset) : context.length;
for(i,j; i<j; i++) {
ret += block(context[i]);
}
return ret;
});
我发现了here
编辑
我正在尝试使用下面的帮助程序,也许我调用它的方式有问题。
<script id="template_marcas" type="text/x-handlebars-template">
{{#slice datos_marcas offset="1" limit="5"}}
<div class="large-3 small-3 columns marca_item">
<div class="marca_content">
<h3>{{img}}</h3>
<p>{{descp}}</p>
<a href="{{href}}">Ver descuentos</a>
</div>
</div>
{{/slice}}
</script>
最佳答案
关于javascript - 带有自定义助手的handlebars.js 模板可对数据数组进行切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26529338/