我正在尝试使用Node.js构建一个完整的web应用程序。是否有一个模板引擎类似于(例如)Django模板引擎或类似的,至少允许你扩展基本模板?


当前回答

如果你喜欢haml,但想要更好的东西,请检查http://jade-lang.com的节点,我也写了haml.js:)

其他回答

Haml是node.js的一个很好的选择

http://github.com/creationix/haml-js

哈姆尔-JS

!!! XML
!!! strict
%html{ xmlns: "http://www.w3.org/1999/xhtml" }
  %head
    %title Sample haml template
  %body
    .profile
      .left.column
        #date= print_date()
        #address= current_user.address
      .right.column
        #email= current_user.email
        #bio= current_user.bio

html

<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Sample haml template
</title></head><body><div class="profile"><div class="left column"><div id="date">January 1, 2009
</div><div id="address">Richardson, TX
</div></div><div class="right column"><div id="email">tim@creationix.com
</div><div id="bio">Experienced software professional...
</div></div></div></body></html>

也试试Yajet吧。;-)这是我昨天刚刚发布的一个新版本,但我现在已经使用了一段时间,它稳定而快速(模板被编译为原生JS函数)。

对于模板引擎来说,它具有IMO最好的语法,尽管代码大小较小(减小了8.5K),但功能集丰富。它的指令允许你引入条件,迭代数组/哈希,定义可重用的模板组件等。

尝试“vash”- net mvc类似razor语法的node.js

https://github.com/kirbysayshi/Vash

也可以查看:http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx


// sample
var tmpl = vash.compile('<hr/>@model.a,@model.b<hr/>');
var html = tmpl({"a": "hello", "b": "world"});
res.write(html);

如果您正在寻找一种极简的模板方法,您可以查看JSON Template。

一个功能更全的替代方案是EJS。它有点类似于从Django中得到的东西。

你的里程可能因人而异——它们是为浏览器Javascript环境设计的,而不是Node.js。

我在Symfony中使用Twig,现在正在尝试node.js,所以我正在寻找https://github.com/justjohn/twig.js和https://github.com/paularmstrong/swig,如果你使用django,你可能会喜欢。