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


当前回答

查看Node js模块wiki页面。他们列出了所有支持node.js的模板引擎。

其他回答

我听说过关于{dust} http://akdubya.github.com/dustjs/#dust的好事情

警告:JinJs不再被维护。它仍在工作,但与最新版本的express不兼容。

你可以试着用jinj。它是Jinja的一个端口,一个非常好的Python模板系统。你可以像这样用npm安装它:

npm install jinjs

在模板。tpl:

I say : "{{ sentence }}"

在你的template.js中:

jinjs = require('jinjs');
jinjs.registerExtension('.tpl');
tpl = require('./template');
str = tpl.render ({sentence : 'Hello, World!'});
console.log(str);

输出将是:

I say : "Hello, World!"

我们正在积极开发,一个好的文档应该很快就会出来。

你应该能够使用mustache.js,如果它不工作,请把问题发给我,我会把它修复,因为我即将在node.js中使用它们。

http://github.com/janl/mustache.js

我知道它可以在没有DOM的情况下工作,因为很多CouchDB独立应用程序在Spidermonkey视图服务器中使用它。

你应该看一看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>

你可以试试无胡子(它的灵感来自焊接/板):

例如:

{ post:
  { title: "Next generation templating: Start shaving!"
  , text: "TL;DR You should really check out beardless!"
  , comments:
    [ {text: "Hey cool!"}
    , {text: "Really gotta check that out..."}  ]
  }
}

你的模板:

<h1 data-template="post.title"></h1>
<p data-template="post.text"></p>
<div>
  <div data-template="post.comments" class="comment">
    <p data-template="post.comments.text"></p>
  </div>
</div>

输出:

<h1>Next generation templating: Start shaving!</h1>
<p>TL;DR You should really check out beardless!</p>
<div>
  <div class="comment">
    <p>Hey cool!</p>
  </div>
  <div class="comment">
    <p>Really gotta check that out...</p>
  </div>
</div>