组装 - 将部分字串作为Handlebars呈现 [英] assemble - Render a list of strings as Handlebars partial

查看:206
本文介绍了组装 - 将部分字串作为Handlebars呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用汇编我确实遇到了一个我无法解决的问题。



我在小部件 YAML-front-matter.htmlrel =nofollow> YAML front matter 部分,并包含部分 {{>预留}} 。直到这里一切都按预期工作!



现在我想要做的是取得列表小部件,并在旁边模板中呈现我的部分。但无论如何它不能按预期工作。


$ b src / templates / layouts / layout-default.hbs $ b

  --- 
布局:src / templates / layouts / layout-default.hbs
部件:
- widget_link-list
- widget_welcome-message
---

< section role =main>
< h1>模板TwoCol< / h1>
{{> body}}
< / section>
<边际角色=互补>
{{>抛开}}
< / aside>

src / templates / partials / aside.hbs

  {{#each widgets}} 
{{。}}
{{/ each}}

使用 {{。}} 将我上面定义的列表打印为字符串。但是,如果我尝试做 {{> 。}} 这样,控制台会发出以下警告:


警告:无法找到使用--force继续。



解决方案

我通过创建一个可以从任何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 =函数(Handlebars,context){

Handlebars.registerHelper(renderPartial,function(name){

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

if(typeof template!=='Function'){
//未编译,所以我们可以安全编译它
fn = Handlebars.compile(template);
} else {

//已编译,只需重新使用它
fn = template;
}

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

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


Using the assemble i actually stuck on a problem which i can't fix myself.

I'm defining a bunch of widgets in the YAML front matter section and including an partial aside {{> aside}}. Until here everything works as expected!

What i'm trying to do now, is to take the list widgets and rendering my partials within the aside template. But anyhow it does not work as expected.

src/templates/layouts/layout-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/templates/partials/aside.hbs

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

Using {{.}} prints my above defined list as string. But if i try to do {{> .}} this, the console drops the following warning:

Warning: The partial . could not be found Use --force to continue.

解决方案

I found a way by creating a custom helper that can be invoked from any Handlebars template. Now i'm able to use {{renderPartial 'partialName' context}}.

In my template:

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

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

Javascript Module

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);
    });
};

这篇关于组装 - 将部分字串作为Handlebars呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆