我使用的NodeJS/Express3和粉尘,LinkedIn和愿意做的相当于:在灰尘模板循环
for(var i = locals.first;i < (locals.first + locals.pages); $i++) {
<div>i</div>
}
我想我需要创建一个辅助功能,但我就怎么做还不清楚所以
我使用的NodeJS/Express3和粉尘,LinkedIn和愿意做的相当于:在灰尘模板循环
for(var i = locals.first;i < (locals.first + locals.pages); $i++) {
<div>i</div>
}
我想我需要创建一个辅助功能,但我就怎么做还不清楚所以
好的。在我的情况下,我试图创建一个寻呼机,但我打算简化答案,以便人们可以得到一般想法。可能有更干净的方法来做到这一点。
我的帮手将被称为'传呼机'。我有一些我想要通过的当地人的快速变量。这些是locals.start和locals.end - 每个代表我的寻呼机开始和结束的数字。我也会传递一个url用于寻呼机。
在我的模板我叫助手用我可能需要的所有参数一起:
{@pager start="{locals.start}" end="{locals.end}" url="/articles?page=%page%"/}
然后在我的应用程序的,我会添加这个帮手。我要确保我的防尘模块存在:
cons = require('consolidate'),
dust = require('dustjs-linkedin'),
然后创建助手:
dust.helpers['pager'] = function(chunk, context, bodies, params) {
//tapping these to get the actual value (since we passed vsaraibles) and casting as a number with '+'
first = +dust.helpers.tap(params.first, chunk, context);
last = +dust.helpers.tap(params.last, chunk, context);
//this one is all ready since we just passed a string
url = params.url;
//build our html - do whatever you need here - this is an example
var html = '<ul>';
for(var i = first; i <= last; i++) {
//I used a replace and placeholder - prob other methods you can use
html += '<li><a href="' + url.replace('%page%', i) + '">' + i '</a></li>';
}
html += '</ul>';
//write this back to the template
chunk.write(html);
//return it
return chunk;
};
希望这可以帮助别人
感谢您的片段,有趣的我其实也试图建立分页,这比做一个简单的循环更好 –
你能给什么的'locals'多了几分清晰度对象应该看起来像? – smfoote
其实我觉得我想通了 - 将发布我的回答 – cyberwombat