Mustache.js和Handlebars.js有什么区别?

本文翻译自:What are the differences between Mustache.js and Handlebars.js?

Major differences I've seen are: 我见过的主要差异是:

  • Handlebars adds #if , #unless , #with , and #each #unless添加了#if#if #unless#if #with#each
  • Handlebars adds helpers 把手增加了助手
  • Handlebars templates are compiled (Mustache can be too) 处理句柄模板(Mustache也可以)
  • Handlebars supports paths 把手支持路径
  • Allows use of {{this}} in blocks (which outputs the current item's string value) 允许在块中使用{{this}} (输出当前项的字符串值)
  • Handlebars.SafeString() (and maybe some other methods) Handlebars.SafeString() (也许还有一些其他方法)
  • Handlebars is 2 to 7 times faster 把手的速度提高了2到7倍
  • Mustache supports inverted sections (ie if !x ... ) 胡子支持倒置部分 (即if !x ...

(Please correct me if I'm wrong with the above.) (如果我对上述内容有误,请纠正我。)

Are there any other major differences I am missing? 我还缺少其他重大差异吗?


#1楼

参考:https://stackoom.com/question/iI3A/Mustache-js和Handlebars-js有什么区别


#2楼

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-handlebarshandlebars.javahandlebars-rubylightncandy(PHP)handlebars-objc等项目


#3楼

Mustache pros: 小胡子职业:

  • Very popular choice with a large, active community. 非常受欢迎的选择与一个大型,活跃的社区。
  • Server side support in many languages, including Java. 服务器端支持多种语言,包括Java。
  • Logic-less templates do a great job of forcing you to separate presentation from logic. 无逻辑模板可以很好地强制您将表示与逻辑分开。
  • Clean syntax leads to templates that are easy to build, read, and maintain. 干净的语法导致易于构建,读取和维护的模板。

Mustache cons: 小胡子缺点:

  • A little too logic-less: basic tasks (eg label alternate rows with different CSS classes) are difficult. 有点太逻辑 - 基本任务(例如标记具有不同CSS类的备用行)是困难的。
  • View logic is often pushed back to the server or implemented as a "lambda" (callable function). 视图逻辑通常被推回到服务器或实现为“lambda”(可调用函数)。
  • For lambdas to work on client and server, you must write them in JavaScript. 要使lambda能够在客户端和服务器上运行,您必须使用JavaScript编写它们。

Handlebars pros: 把手专业人士:

  • Logic-less templates do a great job of forcing you to separate presentation from logic. 无逻辑模板可以很好地强制您将表示与逻辑分开。
  • Clean syntax leads to templates that are easy to build, read, and maintain. 干净的语法导致易于构建,读取和维护的模板。
  • Compiled rather than interpreted templates. 编译而不是解释模板。
  • Better support for paths than mustache (ie, reaching deep into a context object). 比小胡子更好地支持路径(即深入到上下文对象)。
  • Better support for global helpers than mustache. 比小胡子更好地支持全球助手。

Handlebars cons: 把手缺点:

  • Requires server-side JavaScript to render on the server. 需要服务器端JavaScript在服务器上呈现。

Source: The client-side templating throwdown: mustache, handlebars, dust.js, and more 来源: 客户端模板抛出:小胡子,把手,dust.js等等


#4楼

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。


#5楼

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中,你要么必须:

  • use {{this}} instead 请改用{{this}}
  • use a parent path, ie, {{../variable}} to get back out to relevant scope 使用父路径,即{{../variable}}返回相关范围
  • define a child variable value within the parent variable object 在父variable对象中定义子variable

More details on this, if you want them, here . 如果您需要,请在此处详细了解此信息。


#6楼

- 除了使用“this”作为把手,以及用于胡子的变量块中的嵌套变量之外,您还可以使用块中的嵌套点来表示胡子:

    {{#variable}}<span class="text">{{.}}</span>{{/variable}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值