且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-10-26 11:48:58

你几乎已经完成了它,然而胡须模板也可以被编译。 >

小胡子缺少帮手和更高级的方块,因为它试图无逻辑。 Handlebars的自定义助手可能非常有用,但通常最终会在您的模板中引入逻辑。



Mustache有许多不同的编译器(JavaScript,Ruby,Python,C等)。 )。 Handlebars以JavaScript开始,现在有像 django-handlebars handlebars.java handlebars-ruby lightncandy(PHP) handlebars-objc

Major differences I've seen are:

  • Handlebars adds #if, #unless, #with, and #each
  • Handlebars adds helpers
  • Handlebars templates are compiled (Mustache can be too)
  • Handlebars supports paths
  • Allows use of {{this}} in blocks (which outputs the current item's string value)
  • Handlebars.SafeString() (and maybe some other methods)
  • Handlebars is 2 to 7 times faster
  • Mustache supports inverted sections (i.e. if !x ...)

(Please correct me if I'm wrong with the above.)

Are there any other major differences I am missing?

You've pretty much nailed it, however Mustache templates can also be compiled.

Mustache is missing helpers and the more advanced blocks because it strives to be logicless. 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.). Handlebars began in JavaScript, now there are projects like django-handlebars, handlebars.java, handlebars-ruby, lightncandy (PHP), and handlebars-objc.