Node.js-視圖引擎【1】-Swig集成express的安裝與配置


node.js視圖引擎,選來選去發現Swig最符合我的胃口哈哈。

一、安裝Swig視圖引擎

npm install -g swig

二、在node.js代碼中配置如下

var app = require('express')();
var swig = require('swig');

app.engein('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');

三、index.html模板頁

<html>
{{content}}
</html>

四、使用

app.get('/index', function(req, res) {
    res.render('index.html', {content: 'hello, world!'});
});    

瀏覽器輸入/index顯示:hello, world! 

五:工作原理

 Swig reads template files and translates them into cached javascript functions. When we later render a template we call the evaluated function, passing a context object as an argument.

為了提升性能模板默認是會緩存在內存中的,如果修改了模板要重啟node.js實例,當然,也可以通過選項禁用緩存。

 

Swig API幫助文檔:http://paularmstrong.github.io/swig/docs/api/  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM