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


当前回答

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

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

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

其他回答

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

例如:

{ 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>

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

请在这里查看文档。

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>

谷歌的闭包模板是一个原生javascript模板系统,看起来很适合NodeJS。下面是对它们进行积分的一些说明。

你尝试过PURE吗?如果你尝试过,可以随时在论坛上发表你可能遇到的问题

虽然它主要是为浏览器设计的,但它与Jaxer和Rhino配合得很好。

我还不知道node.js,但如果你能在内存中缓存一些JS和函数,速度应该会更令人印象深刻。