Template partials and layouts help you reuse parts of your web pages easily. They keep your code clean and save time by avoiding repetition.
Template partials and layouts in Express
app.set('view engine', 'ejs'); // In your main layout file (e.g., layout.ejs): <!DOCTYPE html> <html> <head> <title><%= title %></title> </head> <body> <%- include('partials/header') %> <main> <%- body %> </main> <%- include('partials/footer') %> </body> </html>
Use <%- include('path/to/partial') %> to insert partial templates.
Layouts usually have a placeholder like <%- body %> where page content goes.
<%- include('partials/navbar') %><!-- layout.ejs -->
<html>
<body>
<%- include('partials/header') %>
<%- body %>
<%- include('partials/footer') %>
</body>
</html><!-- page.ejs -->
<h1>Welcome!</h1>
<p>This is the home page.</p>This Express app uses EJS templates with a layout and partials for header and footer. The index.ejs page uses the layout, which includes the header and footer partials. This keeps the page structure consistent and easy to update.
const express = require('express'); const app = express(); const path = require('path'); app.set('view engine', 'ejs'); app.set('views', path.join(__dirname, 'views')); app.get('/', (req, res) => { res.render('index', {}, (err, indexHtml) => { if (err) { console.log(err); return res.status(500).send('Something went wrong'); } res.render('layout', { title: 'Home Page', body: indexHtml }); }); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); }); // Directory structure: // views/ // layout.ejs // index.ejs // partials/ // header.ejs // footer.ejs // layout.ejs content: // <!DOCTYPE html> // <html lang="en"> // <head> // <meta charset="UTF-8"> // <meta name="viewport" content="width=device-width, initial-scale=1.0"> // <title><%= title %></title> // </head> // <body> // <%- include('partials/header') %> // <main> // <%- body %> // </main> // <%- include('partials/footer') %> // </body> // </html> // index.ejs content: // <h1>Welcome to the Home Page</h1> // <p>This page uses a layout and partials.</p> // partials/header.ejs content: // <header> // <h2>Site Header</h2> // </header> // partials/footer.ejs content: // <footer> // <p>Site Footer © 2024</p> // </footer>
Make sure your partials folder is inside your views folder for easy includes.
Use <%- include() %> to render raw HTML from partials, not escaped text.
Layouts help keep your site consistent and make updates faster.
Template partials let you reuse small parts like headers and footers.
Layouts provide a main structure where page content fits in.
Using both keeps your code clean and your site consistent.
Practice
Solution
Step 1: Understand what partials are
Partials are small pieces of templates like headers or footers that you can reuse.Step 2: Differentiate partials from layouts
Layouts provide the main page structure, while partials are smaller reusable parts inside layouts or pages.Final Answer:
To reuse small template parts like headers or footers across pages -> Option BQuick Check:
Partials = reusable small template parts [OK]
- Confusing partials with layouts
- Thinking partials define full page structure
- Assuming partials handle backend logic
header in an EJS template?Solution
Step 1: Recall EJS partial include syntax
EJS uses<%- include('partialName') %>to include partials with unescaped output.Step 2: Check each option
<%- include('header') %>matches the correct syntax with parentheses and quotes around 'header'.Final Answer:
<%- include('header') %> -> Option AQuick Check:
EJS partial include = <%- include('name') %> [OK]
- Omitting quotes or parentheses in include
- Using <%= instead of <%- for unescaped output
- Confusing syntax with other template engines
app.set('view engine', 'ejs');
// In route handler:
res.render('index', { title: 'Home' });And the layout
layout.ejs contains:<html> <head><title><%= title %></title></head> <body> <%- body %> </body> </html>
If
index.ejs has <h1>Welcome</h1>, what will the rendered HTML look like?Solution
Step 1: Understand layout and body insertion
The layout uses<%- body %>to insert the rendered content ofindex.ejsunescaped.Step 2: Check variable substitution
The title variable is passed as 'Home', so<%= title %>becomes 'Home'. The body inserts<h1>Welcome</h1>as HTML.Final Answer:
<html><head><title>Home</title></head><body><h1>Welcome</h1></body></html> -> Option CQuick Check:
Layout inserts title and body correctly [OK]
- Using escaped output <%= body %> causing HTML to show as text
- Not passing title variable to template
- Assuming body variable is automatic without layout support
ReferenceError: body is not defined. What is the likely cause?Solution
Step 1: Understand how layouts work in Express with EJS
EJS alone does not support layouts natively; you need middleware likeexpress-ejs-layoutsto injectbodycontent.Step 2: Identify cause of ReferenceError
Ifbodyis undefined, it means the layout engine or middleware that setsbodyis missing or not configured.Final Answer:
You forgot to use a layout engine or middleware that injects the body content -> Option AQuick Check:
Missing layout middleware = body undefined error [OK]
- Assuming EJS supports layouts without extra setup
- Confusing escaped vs unescaped output errors
- Ignoring middleware installation or configuration
Solution
Step 1: Understand roles of layouts and partials
Layouts provide the main page structure; partials are reusable pieces like header and footer.Step 2: Combine layout and partials for best practice
Put header and footer partials inside the layout, then insert page-specific content in the body area.Final Answer:
Create a layout.ejs with header and footer partials included, and insert page content in a <%- body %> area -> Option DQuick Check:
Layout + partials = clean, consistent site structure [OK]
- Duplicating header/footer in every page instead of partials
- Skipping layout causing inconsistent page structure
- Mixing content and layout roles improperly
Ready to prove it?
Pass the test and this pattern moves unknown
