在Express.js中使用Swig模板框架

在Express.js中使用Swig模板框架
发布于
# nodejs

Swig是一个node.js和浏览器端的Javascript模板引擎。

在Express.js中使用Swig模板框架

先通过 npm 安装swig :

nbsp;npm install swig --save


Swig可以很容易就兼容Express,Express是一个基于nodejs的简单好用的应用框架。下面就是一个基础的集合Swig和Expressd 两个框架的例子:

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

// This is where all the magic happens!
app.engine('html', swig.renderFile);

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

// Swig 将会默认为你缓存模板文件,但是你能够禁用它,并使用Express来缓存并代替它
// Swig will cache templates for you, but you can disable
// that and use Express's caching instead, if you like:
app.set('view cache', false);

// To disable Swig's cache, do the following:
swig.setDefaults({ cache: false });
// 注意:你应该确保在生产环境中始终使用模板缓存
// NOTE: You should always cache templates in a production environment.
// Don't leave both of these to `false` in production!

app.get('/', function (req, res) {
  res.render('index', { /* template locals context */ });
});

app.listen(1337);
console.log('Application Started on http://localhost:1337/');


找到 0 条评论