In a hogan-express node.js express application, How can I use different template than global template in particular routes? -
OK, so I am new to expressing, templating, and back-ends. I am searching all day to answer this question so that there is no benefit. So, I thought I would ask it here.
How can you use a different template for special routes except for a global template , in the app? I would still like to partially use so that you can keep this in mind if you are referring me to try a different structure or something (not sure how common is partly ...)
code Here is the code I have seen from Dogs for Hogan-Express (It has been replaced with a coffee script. Regular Javascript.)
var Express = Expected ('Express'); var app = express (); Start the example code from // Docs // I like my app JS / Server .js file ap. Use code ('visual engine', 'html'); App.set ('layout', 'layout'); // As I explained above, global template app.set ('partial', {foo: 'foo'}); App.enable ('view cache'); App.engine ('html', Required ('Hogan-Express')); // I have been referred to this function by a different file ... // In my code I will explain below: I app.get ('/', function (req, res) {res.locals = {name: 'Andrew '}; Return res.render (' template ', {partials: {message:' message '}})}); My code is different as follows
// in --- file ---> Requires 'app.js' var routes ('. / Routes / index.js'); App.get ('/', routes.index); // --- in --- file --- & gt; '/routes/index.js' export.index = function (Rick, Race) {var template_data = {posts: blogposts, currentUser: req.user}; Res.render ('index.html', template_data); };
You just add the layout: 'layout 2' Your template_data object is sent as the second parameter in res.render .
// in --- file ---> Requires 'App.js' var routes ('./routes / index.js'); App.get ('/', routes.index); // --- in --- file --- & gt; '/routes/index.js' export.index = function (req, res) {var template_data = {layout: 'layout2', // this is an important addition. Post: blog post, current user: req.user}; Res.render ('index.html', template_data); }; The new layout will work for each route that you declare, but do not overwrite the global template for other routes declared in this fashion.
var Express = Expected ('Express'); var app = express (); Start the example code from // Docs // I like my app JS / Server .js file ap. Use code ('visual engine', 'html'); App.set ('layout', 'layout'); // As I explained above, global template app.set ('partial', {foo: 'foo'}); App.enable ('view cache'); App.engine ('html', Required ('Hogan-Express')); // I have been referred to this function by a different file ... // In my code I will explain below: I app.get ('/', function (req, res) {res.locals = {name: 'Andrew '}; Return res.render (' template ', {partials: {message:' message '}})}); My code is different as follows
// in --- file ---> Requires 'app.js' var routes ('. / Routes / index.js'); App.get ('/', routes.index); // --- in --- file --- & gt; '/routes/index.js' export.index = function (Rick, Race) {var template_data = {posts: blogposts, currentUser: req.user}; Res.render ('index.html', template_data); };
You just add the layout: 'layout 2' Your template_data object is sent as the second parameter in res.render .
// in --- file ---> Requires 'App.js' var routes ('./routes / index.js'); App.get ('/', routes.index); // --- in --- file --- & gt; '/routes/index.js' export.index = function (req, res) {var template_data = {layout: 'layout2', // this is an important addition. Post: blog post, current user: req.user}; Res.render ('index.html', template_data); }; The new layout will work for each route that you declare, but do not overwrite the global template for other routes declared in this fashion.
Comments
Post a Comment