For the homepage of my site using Node.js, how would I go about rendering a layout and the homepage's respective view?
Every page on my site should be using layout.ejs, as this contains the header and footer. Body content for the page's respective view is injected within layout.ejs in this yield section:
<%- body %>
So far using this code, the homepage is rendering the layout.ejs but not index.ejs for the view partial. How do I pass in index.ejs?
app.get('/', function(request, response) {
response.render('layout.ejs');
});
This isn't something EJS can really do as far as I know. You can try rendering you body template separately and pass it as your body variable.
// something we prepared earlier
var body = ejs.compile(fs.readFileSync("body.ejs"));
app.get('/', function(request, response) {
response.render('layout.ejs', {
body: body(someObject)
});
});
If i understand correctly you want to include in every page a header and a footer. In ejs you can include so you can create a header.ejs and and a footer.ejs and in each other page you will write `<% include ./header.ejs %> on the top and <% include ./footer.ejs %> on the bottom. This way each time you load index.ejs for example it will render as it contains header and footer. Unfortunately this is the closest to layout in ejs nowadays.You will be able to render this as simple as you do now:
app.get('/', function(request, response) {
response.render('index.ejs');
});