mustache {{{partial}}} 与 {{>partial}}

标签 mustache handlebars.js

我的 node.js 应用程序中有一个 Handlebars / mustache 布局文件,如下所示:

{{> header}}
{{> navbar}}
{{{body}}}
{{> footer-user}}
{{> footer}}

既然所有五行都包含,那么两种类型的包含之间有什么区别吗?我可以将 {{{body}}} 替换为 {{>body}} 或将 {{> header}} 替换为 {{{header}}} 吗?它们似乎都没有逃脱包含的 html。最佳实践是什么?

最佳答案

语法 {{> name}} 用于部分,即包含另一个模板:

Handlebars allows for template reuse through partials. Partials are normal Handlebars templates that may be called directly by other templates.

{{{name}}} 用于包含数据而不转义它。如果您希望转义数据,可以使用 {{name}}:

Handlebars HTML-escapes values returned by a {{expression}}. If you don't want Handlebars to escape a value, use the "triple-stash", {{{.

所以它们是不同的东西。

{{{body}}} 引用应用程序提供的当前上下文中的 body 属性(或者它可以引用辅助函数,但这不是案例在这里)。

如果代码中有 {{>body}},则意味着您有一个名为 body 的模板,您希望 Handlebars 加载该模板。

请注意,如果您正确使用 Handlebars,则所有部分都需要通过调用 Handlebars.registerPartial 进行注册(如果您的项目使用 express-handlebars,它会完成所有操作为您服务,这样您就不会在代码中找到它)。

Handlebars 的文档可以在 handlebarsjs.com 找到.

关于 mustache {{{partial}}} 与 {{>partial}},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29976892/

相关文章:

javascript - Ember JS : Value assigned through JQuery or JavaScript not reflected in model

javascript - Handlebar.helper 的 View 无法使用 Controller 中的操作

ruby-on-rails - 如何使用 ruby​​ on rails 助手格式化 json 数据?

ember.js - 在 ember.js 中创建自定义未绑定(bind)助手

javascript - 在 Handlebars mustache 内按键访问数组/对象变量

jquery - 使用 html2canvas 和 jspdf 创建的 PDF 文件 - pdf 中不允许选择文本

javascript - 如何从 handlebars.js 部分加载多个模板

jquery - 如何在 jQuery 中的 mustache 模板中添加一个带有图片数量结束增量的类?

javascript - 将 mustache 模板嵌入到另一个模板中

encoding - Handlebars 三重存储以避免转义 html 实体