Swig-NodeJS 介绍
swig
是node端的一个优秀简洁的模板引擎,类似Python模板引擎Jinja,目前不仅在node端较为通用,相对于jade、ejs优秀,而且在浏览器端也可以很好地运行。
特性:
使用示例:
模板代码
<h1>{{ pagename|title }}</h1> <ul> {% for author in authors %} <li{% if loop.first %} class="first"{% endif %}>{{ author }}</li> {% endfor %} </ul>
node.js 代码:
var swig = require('swig'); var template = swig.compileFile('/absolute/path/to/template.html'); var output = template({ pagename: 'awesome people', authors: ['Paul', 'Jim', 'Jane'] });
<h1>Awesome People</h1> <ul> <li class="first">Paul</li> <li>Jim</li> <li>Jane</li> </ul>