2016-05-09 32 views
0

我正試圖解決數據可以包含引用其他數據的標籤的情況。所以,如果我的灰塵文件是這樣的:幫助器中的上下文數據中的標籤

Document.title: {Document.title} 
cfg.title: {cfg.title} 
output: {#ctx key="{cfg.title}" /} 

和我的背景是這樣的:

{ 
    cfg: { 
    title: '{Document.title}' 
    } 
    , Document: { 
    title: 'Here is my title' 
    } 
    , ctx: function(chunk, context, bodies, params){ 
    return context.resolve(params.key); 
    } 
} 

我得到以下輸出:

Document.title: This is the title 
cfg.title: {Document.title} 
output: 

什麼我的「CTX 「助手功能需要看起來像輸出」這是我的標題「? (注意:我知道指向Document.title會更容易些,但是'cfg'和'Document'是從不同的地方生成的,並在渲染時合併)

回答

0

在某些時候,您必須編譯(或以其他方式解析)形成配置的minitemplate。這是一個可行的方法,在這裏我用dust.compile建立預先配置:

{ 
    cfg: { 
    title: dust.compile('{Document.title}') 
    }, Document: { 
    title: 'Here is my title' 
    }, ctx: function(chunk, context, bodies, params){ 
    eval(params.key)(chunk, context); 
    } 
} 

隨着模板:

{#ctx key=cfg.title /} 

注意要傳遞cfg.title直接在這裏,而不是序曲一字符串化它的這很重要,這樣您就不必使用context.resolve來進行雙向查找。

此方法不會生成模板可讀的cfg.title,所以如果這對您很重要,您可以將dust.compile步驟移動到ctx函數中。

+0

這將工作。謝謝! 注意任何人找到這個答案,如果你在2.7之前工作的灰塵,你需要添加第二個參數dust.compile。 –