admin管理员组文章数量:1430604
I have a html file footer.html
that stores a footer of the website and I'd like to reuse it on different pages. How can I include it in the template file template.html
with lodash/underscore? I have read this article about node-partial
but I'm not sure how the module node-partial
could work with render
in Express 4.
var express = require('express')
, app = express()
, http = require('http').createServer(app)
, _ = require('lodash')._
,cons = require('consolidate');
app.engine('html', cons.lodash);
app.set('view engine', 'html')
app.set('views', './views')
app.get('/', function(req, res){
res.render('index.html', {hello: 'Wele !'})
});
Template file
<h1><%= hello %></h1>
<p><%= _('hello') %></p>
<% include './footer.html' %> // Can I add a footer file to the template?
I have a html file footer.html
that stores a footer of the website and I'd like to reuse it on different pages. How can I include it in the template file template.html
with lodash/underscore? I have read this article about node-partial
but I'm not sure how the module node-partial
could work with render
in Express 4.
var express = require('express')
, app = express()
, http = require('http').createServer(app)
, _ = require('lodash')._
,cons = require('consolidate');
app.engine('html', cons.lodash);
app.set('view engine', 'html')
app.set('views', './views')
app.get('/', function(req, res){
res.render('index.html', {hello: 'Wele !'})
});
Template file
<h1><%= hello %></h1>
<p><%= _('hello') %></p>
<% include './footer.html' %> // Can I add a footer file to the template?
Share
Improve this question
edited Dec 20, 2014 at 9:30
RedGiant
asked Dec 20, 2014 at 8:31
RedGiantRedGiant
4,76911 gold badges63 silver badges151 bronze badges
1 Answer
Reset to default 8 +50You can. You need to back out a little bit and add the footer as part of the payload.
var footerHTML;
fs.readFile("./footer.html", function(err, data){
if (err) return console.error(err);
footerHTML = data;
})
app.get('/', function(req, res){
res.render('index.html', {hello: 'Wele !', footer: footerHTML)
});
--
<h1><%= hello %></h1>
<p><%= _('hello') %></p>
<%=footer%>
Alternatively, you could switch to a more powerful templating language. PUG is such a language, and it is supported by default by Express.js.
in PUG you would create files named footer.pug and index.pug. To include the footer in your index page it would be:
h1 ${hello}
p ${hello}
include footer
Note: in pug 2.0 the #{}
syntax is replaced with ${}
to be more consistent with ES6 template string syntax
本文标签: javascriptInclude a footer in a template file with expressjs and lodashunderscoreStack Overflow
版权声明:本文标题:javascript - Include a footer in a template file with express.js and lodashunderscore? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745538387a2662377.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论