Swig-NodeJSNodeJS 模板引擎
swig 是node端的一個優(yōu)秀簡潔的模板引擎,類似Python模板引擎Jinja,目前不僅在node端較為通用,相對于jade、ejs優(yōu)秀,而且在瀏覽器端也可以很好地運行。
特性:
-
支持大多數(shù)主流瀏覽器。
-
表達式兼容性好。
-
面向?qū)ο蟮哪0謇^承。
-
將過濾器和轉(zhuǎn)換應(yīng)用到模板中的輸出。
-
可根據(jù)路勁渲染頁面。
-
支持頁面復用。
-
支持動態(tài)頁面。
-
可擴展、可定制。
使用示例:
模板代碼
<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>
評論
圖片
表情
