将模板渲染为expressjs中的变量 [英] Render template to variable in expressjs
本文介绍了将模板渲染为expressjs中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法将模板渲染为变量而不是输出?
Is there a way to render template to a variable instead to output?
res.render('list.ejs', {
posts: posts
});
像这样的
var list = render('list.ejs', {
posts: posts
});
推荐答案
最简单的方法是传递回调到 res.render
,在你的例子中:
The easiest way to do that is to pass a callback to res.render
, in your example:
res.render('list.ejs', {posts: posts}, function(err, list){
//
});
但是,如果要渲染部分模板以将其包含在另一个模板中,您绝对应该有查看查看部分。
But if you want to render partial templates in order to include them in another template you definitely should have a look at view partials.
这篇关于将模板渲染为expressjs中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文