2014-01-28 22 views
2

使用assemble我實際上停留在我無法修復自己的問題上。assemble - 將字符串列表呈現爲Handlebars部分

我在YAML front matter部分定義了一堆widgets,其中包括部分保留{{> aside}}。直到這裏一切都按預期工作!

我現在想要做的是取得列表小部件,並在旁邊模板中呈現我的部分。但無論如何它不能按預期工作。

的src /模板/佈局/佈局default.hbs

--- 
layout: src/templates/layouts/layout-default.hbs 
widgets: 
    - widget_link-list 
    - widget_welcome-message 
--- 

<section role="main"> 
    <h1>Template TwoCol</h1> 
    {{> body }} 
</section> 
<aside role="complementary"> 
    {{> aside}} 
</aside> 

的src /模板/諧音/ aside.hbs

{{#each widgets}} 
    {{.}} 
{{/each}} 

使用{{.}}打印我上面的定義列表,串。但是,如果我試圖做{{> .}}這,控制檯會發出以下警告:

警告:部分。無法找到使用 - 強制繼續。

回答

5

我找到了一種方法,通過創建一個可以從任何Handlebars模板調用的自定義幫助程序。現在我可以使用{{renderPartial 'partialName' context}}

在我的模板:

var aside = ['my-widget-a','my-widget-b','my-widget-x']; 

{{#each aside}} 
    {{renderPartial this ../this}} 
{{/each}} 

的Javascript模塊

module.exports.register = function (Handlebars, context) { 

    Handlebars.registerHelper("renderPartial", function (name) { 

     var fn, 
      template = Handlebars.partials[name]; 

     if (typeof template !== 'Function') { 
      // not compiled, so we can compile it safely 
      fn = Handlebars.compile(template); 
     } else { 

      // already compiled, just reuse it 
      fn = template; 
     } 

     var output = fn(context).replace(/^\s+/, ''); 

     return new Handlebars.SafeString(output); 
    }); 
}; 
+2

我們也有部分幫助:https://github.com/helpers/handlebars-helper-partial 這將拉動YAML前端在運行時不在文件中,並將其與其他上下文合併。 – doowb

相關問題