扫一扫分享
swig是JS模板引擎,它有如下特点:
swig.init({
allowErrors: false,
autoescape: true,
cache: true,
encoding: 'utf8',
filters: {},
root: '/',
tags: {},
extensions: {},
tzOffset: 0
});
options:
var tpl = swig.compileFile("path/to/template/file.html");
var renderedHtml = tpl.render({ vars: 'to be inserted in template' });
或者
var tpl = swig.compile("Template string here");
var renderedHtml = tpl({ vars: 'to be inserted in template' });
npm install express
npm install consolidate
然后
app.engine('.html', cons.swig);
app.set('view engine', 'html');
Swig浏览器版本的api基本与nodejs版相同,不同点如下:
var template = swig.compile('<p>{% block content %}{% endblock %}</p>', { filename: 'main' });
var mypage = swig.compile('{% extends "main" %}{% block content %}Oh hey there!{% endblock %}', { filename: 'mypage' });
手机预览