2013-11-02 29 views
1

我正在嘗試爲Dust.js實施sprintf幫手。爲此,我需要訪問@sprintf塊助手的內容。該塊可能包含額外的助手或變量,這些助手或變量需要在訪問塊體時解釋 - 這意味着,我需要獲取正文的結果。如何在dust.js中訪問塊助手體?

// JSON context: { name: "Fred" } 
{@sprintf day="Saturday"}Hello {name}, today is %s!{/sprintf} 

如何訪問「Hello Fred,今天是%s!」在我的幫手功能?

回答

1

我結束了使用this gist的代碼片段。 我修改它以適合我的需要。

這裏是我的結果(並回答我自己的問題):

dust.helpers.myHelper = function(chunk, context, bodies, params) { 
    var output = ""; 
    chunk.tap(function (data) { 
    output += data; 
    return ""; 
    }).render(bodies.block, context).untap(); 
    console.log(output); // This will now show the rendered result of the block 
    return chunk; 
} 

這也可以被抽象爲一個單獨的函數:

function renderBlock(block, chunk, context) { 
    var output = ""; 
    chunk.tap(function (data) { 
    output += data; 
    return ""; 
    }).render(block, context).untap(); 
    return output; 
} 

dust.helpers.myHelper = function(chunk, context, bodies, params) { 
    var output = renderBlock(bodies.block, chunk, context); 
    console.log(output); // This will now show the rendered result of the block 
    return chunk; 
} 
相關問題