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


当前回答

这里有几个很好的评估引擎 http://engineering.linkedin.com/frontend/client-side-templating-throwdown-mustache-handlebars-dustjs-and-more

其他回答

我已经为Simon Willisons Django项目做了一些相当完整的Django模板语言移植工作(node.js的实用程序函数借用了Django的一些有用概念)。

请在这里查看文档。

你应该看一看node-asyncEJS,它的设计明确考虑了node.js的异步特性。它甚至允许在模板中使用异步代码块。

下面是一个文档示例:

<html>
  <head>
    <% ctx.hello = "World";  %>
    <title><%= "Hello " + ctx.hello %></title>
  </head>
  <body>

    <h1><%? setTimeout(function () { res.print("Async Header"); res.finish(); }, 2000)  %></h1>
    <p><%? setTimeout(function () { res.print("Body"); res.finish(); }, 1000)  %></p>

  </body>
</html>

查看Node js模块wiki页面。他们列出了所有支持node.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>

尝试“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);