Why does express-ejs-layout fails to render layout.ejs? -
in configuration below, contents of home.ejs
not appear plugged <%- body %>
of layout.ejs
page. hitting default route /
returns contents of home.ejs
none of structure or styling in layout.ejs
.
i couldn't find useful information troubleshoot how express-ejs-layouts
work. thoughts on why might be?
app structure:
. ├── app ├── main.js ├── node_modules ├── package.json └── public ├── css │ └── style.css ├── js │ └── app.js └── views ├── layout.ejs └── pages ├── events.ejs ├── home.ejs └── test.ejs
configuration snippet:
// file: app/server.js // template engine app.set('view engine', 'ejs'); app.use(expresslayouts); // set views directory app.set('views', __dirname + '/../public/views'); app.use(express.static(__dirname + '/../public'));
route:
//file: app/controllers/maincontroller.js exports.home = function(req,res){ res.render('pages/home'); }
ejs files:
layout.js (snippet)
<!--file: public/views/layout.ejs --> ... <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-bvyiisifek1dgmjrakycuhahrg32omucww7on3rydg4va+pmstsz/k68vbdejh4u" crossorigin="anonymous"> <link rel="stylesheet" href="../css/style.css"> ... <main id="site-main"> <div class="container"> <%- body %> </div> </main> ...
home.ejs
<!--file: public/views/pages/home.ejs --> home page
edit 1:
my example above didn't highlight declaring route app.use('/',router)
before use.app(expresslayouts)
. swapping around fixed problem.
i set simple express server using file structure. if request home page client, receive:
<main id="site-main"> <div class="container"> home page </div> </main>
this want, right? content of home.ejs
included in layout.ejs
.
i wonder why it's not working you. simplified server.js
used (note no special routing file):
var express = require('express'); var expresslayouts = require('express-ejs-layouts'); var app = express(); app.set('view engine', 'ejs'); app.use(expresslayouts); app.set('views', __dirname + '/../public/views'); app.use(express.static(__dirname + '/../public')); app.get('/', function(req, res) { res.render('pages/home'); }); app.listen(3000);
my dependencies in package.json
:
{ "ejs": "^2.5.2", "express": "^4.14.0", "express-ejs-layouts": "^2.2.0" }
tell me if works you, can research what's problem configuration.
edit: turned out op put route declaration app.use('/',router);
before app.use(expresslayouts);
that's why layout.ejs
not considered rendering.
Comments
Post a Comment