![](/img/trans.png)
[英]How do I use Hogan-Express to compile an email template in an Express application?
[英]In a hogan-express node.js express application, How can I use different template than global template in particular routes?
好吧,我是新来表达,模板和后端的人。 我一直在全天搜寻以回答这个问题,但无济于事。 所以,我想我会在这里问它。
除了在hogan-express , node.js和express应用程序中声明的全局模板之外,如何为特定的路由使用其他模板 ? 我仍然想使用局部函数,因此如果您要我尝试其他框架或其他方法,请记住这一点。 (虽然不确定部分有多常见...)
这是我从文档本身中寻找的Hogan-express代码(它已从coffescript转换为常规javascript。)
var express = require('express');
var app = express();
// start example code from docs.
// I use code like this in my app.js/server.js file
app.set('view engine', 'html');
app.set('layout', 'layout'); // the global template as I explained above.
app.set('partials', {
foo: 'foo'
});
app.enable('view cache');
app.engine('html', require('hogan-express'));
// I have this function referenced from a different file...
// in my code I'll explain below.
app.get('/', function(req, res) {
res.locals = {
name: 'Andrew'
};
return res.render('template', {
partials: {
message: 'message'
}
});
});
我的代码如下
// in---file---> 'app.js'
var routes = require('./routes/index.js');
app.get('/', routes.index);
// ---in---file---> '/routes/index.js'
exports.index = function(req,res){
var template_data = {
posts : blogposts,
currentUser : req.user
};
res.render('index.html', template_data);
};
您只需在您的template_data对象中添加layout:'layout2'
作为第二个参数发送到res.render
。
// in---file---> 'app.js'
var routes = require('./routes/index.js');
app.get('/', routes.index);
// ---in---file---> '/routes/index.js'
exports.index = function(req,res){
var template_data = {
layout:'layout2', // this is the important addition.
posts : blogposts,
currentUser : req.user
};
res.render('index.html', template_data);
};
新的布局将适用于您在其中声明的每个路由,但不会覆盖未以这种方式声明的其他路由的全局模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.