wig.js 和 Twig(PHP) 和 jinja2(Python) 模板引擎很像,用起来很舒服
文档 https://github.com/twigjs/twig.js/wiki
Twig PHP https://twig.symfony.com/
安装
cnpm install twig--save
node中使用
var Twig = require('twig').twig var template = Twig({ data: 'My name is {{ name }}' }); var ret = template.render({ name: 'Tom' }) console.log(ret); // My name is Tom
express中使用
目录
. ├── index.js ├── package.json └── views └── index.twig
package.json
{ "scripts": { "start": "node index.js", "dev": "nodemon index.js" }, "dependencies": { "express": "^4.17.1", "twig": "^1.15.1", "nodemon": "^2.0.3" } }
index.js
express = require('express') app = express(); // 配置 app.set("twig options", { allow_async: true, strict_variables: false }); app.get('/', function (req, res) { res.render('index.html', { name: "Tom" }); }); app.listen(9999);
views/index.twig
My name is {{ name }}