我正在尝试使用Node.js构建一个完整的web应用程序。是否有一个模板引擎类似于(例如)Django模板引擎或类似的,至少允许你扩展基本模板?
当前回答
尝试“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);
其他回答
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>
如果您正在寻找一种极简的模板方法,您可以查看JSON Template。
一个功能更全的替代方案是EJS。它有点类似于从Django中得到的东西。
你的里程可能因人而异——它们是为浏览器Javascript环境设计的,而不是Node.js。
一直都有新的模板引擎。
js为js添加了大量的函数式编程支持,并且有模板。
就在今天,我听说了这个:http://github.com/SamuraiJack/Shotenjin-Joosed
你尝试过PURE吗?如果你尝试过,可以随时在论坛上发表你可能遇到的问题
虽然它主要是为浏览器设计的,但它与Jaxer和Rhino配合得很好。
我还不知道node.js,但如果你能在内存中缓存一些JS和函数,速度应该会更令人印象深刻。
警告: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!"
我们正在积极开发,一个好的文档应该很快就会出来。