小胡子{{{partial}}}与{{>部分的}} [英] mustache {{{partial}}} vs {{> partial}}

查看:122
本文介绍了小胡子{{{partial}}}与{{>部分的}}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的node.js应用程序中有一个车把/小胡子布局文件,如下所示:

I have a handlebars/mustache layout file in my node.js application that looks like this:

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

由于所有五行都包含,两种包含类型之间是否有区别?我可以用{{> body}}替换{{{body}}}还是用{{{header}}}替换{{> header}}?它们似乎都无法逃脱包含的html.最佳做法是什么?

Since all five lines are includes, is there any difference between the two types of includes? Could I replace {{{body}}} with {{>body}} or {{> header}} with {{{header}}}? They both appear not to escape the html that gets included. What's the best practice?

推荐答案

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

The syntax {{> name}} is for partials, i.e. to include another template:

车把允许通过局部使用模板.部分是 可以由其他人直接调用的普通把手模板 模板.

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

{{{name}}}用于包含数据但不转义.如果您希望转义数据,请使用{{name}}:

whereas {{{name}}} is for including data without escaping it. If you wanted the data to be escaped, you'd use {{name}}:

Handlebars {{expression}}返回的HTML转义值.如果你 不想让把手逃脱一个值,请使用三重隐藏" {{{.

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

所以它们是不同的东西.

So they are different things.

{{{body}}}指的是应用程序提供的当前上下文中的body属性(或者,它可以指代辅助函数,但实际情况并非如此).

{{{body}}} refers to a body property in the current context provided by your application (alternatively it could refer to a helper function but that's not the case here).

如果代码中包含{{>body}},则意味着您要加载一个名为body的模板.

If you had {{>body}} in the code, it would mean you have a template named body that you want handlebars to load.

请注意,如果您正确使用了Handlebars,则需要通过调用Handlebars.registerPartial来注册所有部分(如果您的项目使用express-handlebars,则它会为您完成所有操作,因此您不会在代码中找到它).

Note that, if you use Handlebars proper, all partials need to be registered by calling Handlebars.registerPartial (if your project uses express-handlebars, it does it all for you so you won't find this in the code).

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

Handlebars's documentation can be found at handlebarsjs.com.

这篇关于小胡子{{{partial}}}与{{>部分的}}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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