0
나는 NodeJS
과 ExpressJS
을 처음 사용합니다. 나는 사용자 정의 for
통과를 통해 NodeJS
데이터를 통해 index
을 통해 for
루프처럼 사용하고 싶습니다.핸들 막대 템플릿의 사용자 정의 루프를 만드는 방법
아래 코드를 NodeJS
에서 확인하십시오. 여기서 역할 User
의 모든 사용자를 얻고 사용자를 handlebars
보기로 전달합니다.
exports.getAll = function (req, res) {
models.Users.findAll({
where: {
role: {$ne: "User"}
}
}).then(users => {
return res.render('users/index', {users: users});
}).catch(error => {
return res.json(error);
});
};
체크 아웃 내 핸들바보기. 아래 코드에서 모든 사용자를 탐색 할 수 있지만 그런 식으로 사용하고 싶지는 않습니다.
은 내가 도우미 함수를 만드는 시도 for(index = 0; index < count(users); index++
<table class="table table-hover table-light">
<thead>
<tr class="uppercase">
<th class="col-md-2"> Login </th>
<th class="col-md-6"> Description </th>
<th class="col-md-2"> </th>
</tr>
</thead>
<tbody>
{{#each users}}
<tr>
<td> {{username}} </td>
<td> {{about_me}} </td>
<td>
<button type="button" id="#basic" data-toggle="modal" href="#basic" class="btn btn-blue btn-xs">Edit</button>
<button type="button" class="btn btn-danger btn-xs">Remove</button>
</td>
</tr>
{{/each}}
</tbody>
</table>
우리가 정상 for
루프를 사용하는 것처럼 사용하고 싶습니다.
hbs.registerHelper('for', function(from, to, incr, block) {
var accum = '';
for(var i = from; i < to; i += incr)
accum += block.fn(i);
return accum;
});
가 handlebars
템플릿 엔진에서 정상 for
루프를 만들 수 있습니다 코드 아래 확인?
누구나 새로운 도우미 기능을 만들 수 있습니까? 이 도움이
당신이 만든'for' 도우미 함수에 어떤 문제가 있습니까? 왜 다른 것을 만들고 싶니? –
내가보기에 –
처럼'for (index = 0; index