我相信我需要一个自定义Handlebars.js Block Helper来处理基于Bootstrap的网格系统中的行。我希望每{3}项都包含在<div class="row"></div>
<div class="row">
<div class="col-sm-4">...</div>
<div class="col-sm-4">...</div>
<div class="col-sm-4">...</div>
</div>
{{#employees}}
<div class="col-sm-4">
<strong>{{name}}</strong><br>
{{title}}<br>
{{skills}}
</div>
{{/employees}}
var data = {
"employees" : [
{
"name":"Fred Flintstone",
"title":"Frontend Developer",
"skills" : "html,css,javascript"
},
{
"name":"Sally Struthers",
"title":"Frontend Developer",
"skills" : "html,css,javascript"
},
{
"name":"Ben Wilson",
"title":"Frontend Developer",
"skills" : "html,css,javascript"
},
{
"name":"Julie Milson",
"title":"Frontend Developer",
"skills" : "html,css,javascript"
},
{
"name":"Mike Barton",
"title":"Frontend Developer",
"skills" : "html,css,javascript"
}
]
}
这就是我的想法,但需要帮助:)
// pass data and how many per row
Handlebars.registerHelper('gridWrap', function(data,perRow) {
var wrapper = "";
// need index
if(index == 0 || index % perRow == 0) {
wrapper += '<div class="row">';
}
if((index + 1) % perRow == 0 || (index + 1) == data.length) {
wrapper += '</div>';
}
return wrapper;
});
然后一旦构造了帮助器,你将如何在html中使用它?
{{#employees}}
{{#gridWrap employees 3}} {{wrapper}} {{/gridWrap}}
<div class="col-sm-4">
<strong>{{name}}</strong><br>
{{title}}<br>
{{skills}}
</div>
{{#gridWrap employees 3}} {{wrapper}} {{/gridWrap}}
{{/employees}}
答案 0 :(得分:13)
确定:)找到答案here in this killer post!
这个助手正是我想要的。以下是我最终的结果:)
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;
});
{{#grouped_each 3 employees}}
<div class="row">
{{#each this }}
<div class="col-sm-4 item">
<strong>{{name}}</strong><br>
{{title}}<br>
{{skills}}
</div>
{{/each}}
</div>
{{/grouped_each}}
答案 1 :(得分:0)
Bootstrap列在12列之后自动换行。问题是我们失去了该行的底部边缘。
虽然你提供的答案可能有用,但它非常复杂。
另一种更简单的解决方案是在列中添加底部边距。