码迷,mamicode.com
首页 > 其他好文 > 详细

前端模板Nunjucks简介

时间:2017-03-30 21:59:24      阅读:603      评论:0      收藏:0      [点我收藏+]

标签:named   const   config   http   输出   nbsp   htm   index   anim   

参考资料: https://mozilla.github.io/nunjucks/

https://mozilla.github.io/nunjucks/templating.html

https://mozilla.github.io/nunjucks/api.html

 

常用API

render

nunjucks.render(name, [context], [callback])

var res = nunjucks.render(‘foo.html‘);
var res = nunjucks.render(‘foo.html‘, { username: ‘James‘ });    
nunjucks.render(‘async.html‘, function(err, res) {
});

 

renderString

nunjucks.renderString(str, context, [callback]) 返回渲染后的原始字符串。

var res = nunjucks.renderString(‘Hello {{ username }}‘, { username: ‘James‘ });     //输出 Hello James

 

configure

nunjucks.configure([path], [opts]); path 默认当前路径,模板文件所在的路径。

nunjucks.configure(‘views‘, {       //views为模板文件所在的目录
    autoescape: true,
    express: app,
    watch: true
});

 

getTemplate

env.getTemplate(name, [eagerCompile], [callback]) Retrieve the template named name

var tmpl = env.getTemplate(‘page.html‘, true);          //返回模板文件 page.html 的内容

 

express

env.express(app) 用做服务端渲染

var app = express();
env.express(app);

app.get(‘/‘, function(req, res) {
    res.render(‘index.html‘);
});

 

constructor

new Template(src, [env], [path], [eagerCompile])

var tmpl = new nunjucks.Template(‘Hello {{ username }}‘);
tmpl.render({ username: "James" }); // -> "Hello James"

 

前端模板Nunjucks简介

标签:named   const   config   http   输出   nbsp   htm   index   anim   

原文地址:http://www.cnblogs.com/zourong/p/6648364.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!