有什么好用的JS模板引擎吗?

有什么好用的JS模板引擎吗?
比如说 handlebars
或者 augularjs 这些的模板引擎,个人感觉都不是特别好用啊

有没有什么比较好用的模板引擎?

阅读 17.1k
27 个回答

试一下artTemplate

个人觉得萝卜青菜各有所爱,根据需求和喜好加以选择,多多尝试。
如果写过JSP或PHP的话,喜欢<% %>,必然推荐TJ大神的EJS给你,地址:https://github.com/tj/ejs
如果写过Python,喜欢简洁,优美的格式,推荐TJ大神的另一款力作Jade,地址:https://github.com/jadejs/jade

LinkedIn把他们转template engine的经验给blog了下来,
post里边的数据可能已经outdated了,不过还是很有參考价值的,楼主可以参考一下:
The client-side templating throwdown: mustache, handlebars, dust.js, and more

他们的测评准则:

  • DRY: how DRY is the templating technology? is there support for code-reuse and partials?

  • i18n: is there support for translations and multiple languages?

  • Hot reload: are changes visible immediately or is there a compile/deploy cycle?

  • Performance: how long does it take to render in the browser and server?

  • Ramp-up time: how is the learning curve?

  • Ramped-up productivity: once you've ramped-up, how fast can you build things?

  • Server/client support: can the same template be rendered both client-side and server-side?

  • Community: is there an active community using this project? Can you google issues?

  • Library agnostic: are there dependencies on other JS libraries, such as jQuery or Mootools?

  • Testable: how hard is it to write unit and integration tests?

  • Debuggable: is it possible to step through the code while it's running to track down errors?

  • Editor support: is there an editor with auto-complete, syntax highlighting, error checking, etc?

  • Maturity: is this a relatively stable project or still experimenting and churning?

  • Documentation: how is the documentation?

  • Code documentation: do the templates encourage/require documentation/comments?

个人比较常用handlebars,因为不想view里面有太多logical的东西,
而且HBS可以precompile,还可以自由选择在server或是client side render
最后就是handlebars跟jq一点关系都没有..

dot.js 速度快,这是有评测数据的,同时支持在模版中加入js语法。

vue.js 国人做的框架 和angular类似 不过精简了很多。中文文档非常全。

如果纯用模板的话,推荐doT,速度快,支持编译成函数。如果想高级的推荐 riotjs 2.0 、react, 这两个是 webcomponent 概念了,比模板更高级点。

我也是刚接触AngularJS,觉得用起来还不是很熟练,与以前用的Jquery等都不一样,但是AngularJS在当下来说是非常流行的,它的确少去了像Jquery的DOM操作。其实每一个框架或模版都有优劣,也没有所谓的好用或不好用,只有熟不熟练!

如果你的业务不是极端注重效率,推荐nunjucks

java,python,node三种语言的许多模板我都用过,还是觉得python中jinja2的设计最舒服,nunjucks是模仿的jinja2,虽然模拟的不彻底,但是也很不错了。

(swig和nunjucks差不多,而且语法的容错性比nunjucks稍好,虽然效率稍差一丁点,但是因为很久不更新了,不建议了就;
其实这两种模板相当于给jinja2做了个旁证,如果设计不好,也不会不约而同都去模仿~~)

推荐一个我觉得很牛逼的riotjs,你看看它1.x的版本里面有个简洁的模板实现,大概就10行左右的代码

riot.render = function(tmpl, data, escape_fn) {
    if (escape_fn === true) escape_fn = default_escape_fn;
    tmpl = tmpl || '';
    return (FN[tmpl] = FN[tmpl] || new Function("_", "e", "try { return '" +
        tmpl.replace(/[\\\n\r']/g, function(char) {
            return template_escape[char];
        }).replace(/{\s*([\w\.]+)\s*}/g, "' + (e?e(_.$1,'$1'):_.$1||(_.$1==undefined?'':_.$1)) + '") + "' } catch(e) { return '' }")
    )(data, escape_fn);
};

模板引擎? 看一下 layer的吧,推荐

React其实就是模板引擎

直接比较就行了
http://jsperf.com/hadlebars-vs-hogan-vs-mustache/17

如果想运行看结果你得能访问google啊,jquery来自google,或者你copy一份,加个test,改下链接运行一把,也能看测试效果,如果你不是很追求效率的话,其实哪个觉得用得上手,用哪个,模板用起来都不会差别很大

你应该写一下你的需求?你想要什么要求的,才能做推荐。不然我觉得angular就很好,社区成熟,不心烦。所以你要说出你的需求?

谁说handlebars基于jq - -

artTemplate
这个不错,我现在也是用的这个.

非10万以上的数据量,基本都差不多啦,哪个都一样,看上手难度了。

我选用了Swig,因为它的语法与PHP的Twig兼容,而我的网站程序既有JavaScript又有PHP。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题