gpt4 book ai didi

gruntjs - assemble - 将字符串列表呈现为 Handlebars 部分

转载 作者:行者123 更新时间:2023-12-02 04:38:31 25 4
gpt4 key购买 nike

使用 assemble我实际上遇到了一个我自己无法解决的问题。

我在 YAML front matter 中定义了一堆 widgets部分并包括部分 aside {{> aside}}。直到这里,一切都按预期工作!

我现在要做的是获取列表小部件并在 aside 模板中呈现我的部分。但无论如何它没有按预期工作。

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

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

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

最佳答案

我通过创建一个可以从任何 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);
});
};

关于gruntjs - assemble - 将字符串列表呈现为 Handlebars 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21403594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com