本文翻译自:What are the differences between Mustache.js and Handlebars.js?
Major differences I've seen are: 我见过的主要差异是:
#if
, #unless
, #with
, and #each
#unless
添加了#if
, #if
#unless
, #if
#with
和#each
{{this}}
in blocks (which outputs the current item's string value) 允许在块中使用{{this}}
(输出当前项的字符串值) Handlebars.SafeString()
(and maybe some other methods) Handlebars.SafeString()
(也许还有一些其他方法) if !x ...
) 胡子支持倒置部分 (即if !x ...
) (Please correct me if I'm wrong with the above.) (如果我对上述内容有误,请纠正我。)
Are there any other major differences I am missing? 我还缺少其他重大差异吗?
参考:https://stackoom.com/question/iI3A/Mustache-js和Handlebars-js有什么区别
You've pretty much nailed it, however Mustache templates can also be compiled. 你几乎已经钉了它,但是也可以编译Mustache模板。
Mustache is missing helpers and the more advanced blocks because it strives to be logicless. Mustache缺少帮助程序和更高级的块,因为它力求无逻辑。 Handlebars' custom helpers can be very useful, but often end up introducing logic into your templates. 把手的自定义助手可能非常有用,但最终会在您的模板中引入逻辑。
Mustache has many different compilers (JavaScript, Ruby, Python, C, etc.). Mustache有许多不同的编译器(JavaScript,Ruby,Python,C等)。 Handlebars began in JavaScript, now there are projects like django-handlebars , handlebars.java , handlebars-ruby , lightncandy (PHP) , and handlebars-objc . Handlebars开始于JavaScript,现在有django-handlebars , handlebars.java , handlebars-ruby , lightncandy(PHP)和handlebars-objc等项目 。
Mustache pros: 小胡子职业:
Mustache cons: 小胡子缺点:
Handlebars pros: 把手专业人士:
Handlebars cons: 把手缺点:
Source: The client-side templating throwdown: mustache, handlebars, dust.js, and more 来源: 客户端模板抛出:小胡子,把手,dust.js等等
NOTE: This answer is outdated. 注意: 此答案已过时。 It was true at the time it was posted, but no longer is. 它在发布时确实如此,但不再是。
Mustache has interpreters in many languages, while Handlebars is Javascript only. Mustache有多种语言的口译员,而Handlebars只有Javascript。
One subtle but significant difference is in the way the two libraries approach scope. 一个微妙但重要的区别在于两个图书馆接近范围的方式。 Mustache will fall back to parent scope if it can't find a variable within the current context; 如果在当前上下文中找不到变量,则Mustache将回退到父作用域; Handlebars will return a blank string. 把手将返回一个空白字符串。
This is barely mentioned in the GitHub README, where there's one line for it: 这在GitHub README中几乎没有提及,其中有一行:
Handlebars deviates from Mustache slightly in that it does not perform recursive lookup by default. Handlebars略微偏离Mustache,因为默认情况下它不执行递归查找。
However, as noted there, there is a flag to make Handlebars behave in the same way as Mustache -- but it affects performance. 然而,如前所述,有一个标志可以使Handlebars的行为与Mustache相同 - 但它会影响性能。
This has an effect on the way you can use #
variables as conditionals. 这对可以使用的方法的效果#
变量,如条件。
For example in Mustache you can do this: 例如,在Mustache中,您可以这样做:
{{#variable}}<span class="text">{{variable}}</span>{{/variable}}
It basically means "if variable exists and is truthy, print a span with the variable in it". 它基本上意味着“如果变量存在并且是真实的,则打印带有变量的跨度”。 But in Handlebars, you would either have to: 但在Handlebars中,你要么必须:
{{this}}
instead 请改用{{this}}
{{../variable}}
to get back out to relevant scope 使用父路径,即{{../variable}}
返回相关范围 variable
value within the parent variable
object 在父variable
对象中定义子variable
值 More details on this, if you want them, here . 如果您需要,请在此处详细了解此信息。
- 除了使用“this”作为把手,以及用于胡子的变量块中的嵌套变量之外,您还可以使用块中的嵌套点来表示胡子:
{{#variable}}<span class="text">{{.}}</span>{{/variable}}