2015-08-14 106 views
0

我想抽象一些代碼,並希望利用dust.helpers渲染一個parial。DustJS:渲染部分通過幫助器

我的當前設置:

{> "includes/components/link" /} 

理想設置:

{@uiComponent name="link" /} 

我的助手:

dust.helpers.uiComponent = function (chunk, context, bodies, params) { 
    return dust.render('includes/components/' + name, context, function (err, out) { 
     chunk.end(out); 
    }); 
}; 

我也嘗試了一些其他的東西,沒有用。

是的,我試着看文檔。 :(

任何建議,將不勝感激!

+0

你要處理的部分以一種特殊的方式還是這只是抽象掉 – Interrobang

+0

主要是抽象的文件路徑的路徑...但目前想擁有這個例如,如果需要,可以操縱數據。 – peduarte

回答

1

灰塵,助手返回塊,所以你需要使用大塊的方法來恢復你的幫手,而不是dust.render

在這種情況下,你與泛音的工作,所以你要chunk.partial:?!

dust.helpers.uiComponent = function (chunk, context, bodies, params) { 
    var name = context.resolve(params.name); 
    return chunk.partial('includes/components/' + name, context, params); 
}; 
+0

非常感謝,會給這個去! – peduarte

+0

什麼是'context.resolve'? – peduarte

+0

它評估一個參數,如果你沒有傳遞一個字符串,而是傳遞類似'name =「{foo}」' – Interrobang