Bird
Raised Fist0
Expressframework~10 mins

Template partials and layouts in Express - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to render a view using a layout in Express with EJS.

Express
app.get('/', (req, res) => {
  res.render('index', { layout: '[1]' });
});
Drag options to blanks, or click blank then click option'
A'main-layout'
B'index-layout'
C'default-layout'
D'base-layout'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to pass the layout option.
Using a layout name that does not exist.
2fill in blank
medium

Complete the EJS code to include a partial header template inside a layout.

Express
<header>
  <%- include([1]) %>
</header>
Drag options to blanks, or click blank then click option'
A'partials/footer.ejs'
B'partials/sidebar.ejs'
C'layouts/main.ejs'
D'partials/header.ejs'
Attempts:
3 left
💡 Hint
Common Mistakes
Including the wrong partial file.
Forgetting quotes around the partial path.
3fill in blank
hard

Fix the error in the EJS layout to correctly render the body content.

Express
<body>
  [1]
</body>
Drag options to blanks, or click blank then click option'
A<%== body %>
B<%= body %>
C<%- body %>
D<% body %>
Attempts:
3 left
💡 Hint
Common Mistakes
Using <%= body %> which escapes HTML.
Using <% body %> which does not output content.
4fill in blank
hard

Fill both blanks to create a layout that includes a header partial and renders the body content.

Express
<html>
  <body>
    <header>
      <%- include([1]) %>
    </header>
    <main>
      [2]
    </main>
  </body>
</html>
Drag options to blanks, or click blank then click option'
A'partials/header.ejs'
B<%- body %>
C<%= body %>
D'partials/footer.ejs'
Attempts:
3 left
💡 Hint
Common Mistakes
Using escaped output for body content.
Including the wrong partial file.
5fill in blank
hard

Fill all three blanks to create an Express route that renders a view with a layout and includes a footer partial in the layout.

Express
app.get('/about', (req, res) => {
  res.render('about', { layout: [1] });
});

// In layout file:
<footer>
  <%- include([2]) %>
</footer>

// In about.ejs:
<h1>[3]</h1>
Drag options to blanks, or click blank then click option'
A'main-layout'
B'partials/footer.ejs'
CAbout Us
D'partials/header.ejs'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong layout name.
Including header instead of footer partial.
Forgetting quotes around strings.

Practice

(1/5)
1. What is the main purpose of using template partials in Express apps?
easy
A. To define the main layout structure for all pages
B. To reuse small template parts like headers or footers across pages
C. To write JavaScript logic inside templates
D. To connect the app to a database

Solution

  1. Step 1: Understand what partials are

    Partials are small pieces of templates like headers or footers that you can reuse.
  2. Step 2: Differentiate partials from layouts

    Layouts provide the main page structure, while partials are smaller reusable parts inside layouts or pages.
  3. Final Answer:

    To reuse small template parts like headers or footers across pages -> Option B
  4. Quick Check:

    Partials = reusable small template parts [OK]
Hint: Partials = small reusable pieces like header/footer [OK]
Common Mistakes:
  • Confusing partials with layouts
  • Thinking partials define full page structure
  • Assuming partials handle backend logic
2. Which syntax correctly includes a partial named header in an EJS template?
easy
A. <%- include('header') %>
B. <%= partial('header') %>
C. <% include header %>
D. <@include header>

Solution

  1. Step 1: Recall EJS partial include syntax

    EJS uses <%- include('partialName') %> to include partials with unescaped output.
  2. Step 2: Check each option

    <%- include('header') %> matches the correct syntax with parentheses and quotes around 'header'.
  3. Final Answer:

    <%- include('header') %> -> Option A
  4. Quick Check:

    EJS partial include = <%- include('name') %> [OK]
Hint: EJS partials use <%- include('name') %> syntax [OK]
Common Mistakes:
  • Omitting quotes or parentheses in include
  • Using <%= instead of <%- for unescaped output
  • Confusing syntax with other template engines
3. Given this Express setup using EJS with a layout:
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?
medium
A.

Welcome

B. Home<h1>Welcome</h1>
C. Home

Welcome

D. Error: body is undefined

Solution

  1. Step 1: Understand layout and body insertion

    The layout uses <%- body %> to insert the rendered content of index.ejs unescaped.
  2. Step 2: Check variable substitution

    The title variable is passed as 'Home', so <%= title %> becomes 'Home'. The body inserts <h1>Welcome</h1> as HTML.
  3. Final Answer:

    <html><head><title>Home</title></head><body><h1>Welcome</h1></body></html> -> Option C
  4. Quick Check:

    Layout inserts title and body correctly [OK]
Hint: Layout inserts body unescaped with <%- body %> [OK]
Common Mistakes:
  • Using escaped output <%= body %> causing HTML to show as text
  • Not passing title variable to template
  • Assuming body variable is automatic without layout support
4. You try to use a layout in Express with EJS but get an error: ReferenceError: body is not defined. What is the likely cause?
medium
A. You forgot to use a layout engine or middleware that injects the body content
B. You used <%= body %> instead of <%- body %>
C. You named your layout file incorrectly
D. You did not install EJS package

Solution

  1. Step 1: Understand how layouts work in Express with EJS

    EJS alone does not support layouts natively; you need middleware like express-ejs-layouts to inject body content.
  2. Step 2: Identify cause of ReferenceError

    If body is undefined, it means the layout engine or middleware that sets body is missing or not configured.
  3. Final Answer:

    You forgot to use a layout engine or middleware that injects the body content -> Option A
  4. Quick Check:

    Missing layout middleware = body undefined error [OK]
Hint: Layouts need middleware to define body variable [OK]
Common Mistakes:
  • Assuming EJS supports layouts without extra setup
  • Confusing escaped vs unescaped output errors
  • Ignoring middleware installation or configuration
5. You want to create a consistent site with a header, footer, and main content area using Express and EJS. Which approach best uses partials and layouts together?
hard
A. Use only partials for header, footer, and content without any layout file
B. Include header and footer partials directly in every page template without a layout
C. Write all HTML in each page template without partials or layouts
D. Create a layout.ejs with header and footer partials included, and insert page content in a <%- body %> area

Solution

  1. Step 1: Understand roles of layouts and partials

    Layouts provide the main page structure; partials are reusable pieces like header and footer.
  2. 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.
  3. Final Answer:

    Create a layout.ejs with header and footer partials included, and insert page content in a <%- body %> area -> Option D
  4. Quick Check:

    Layout + partials = clean, consistent site structure [OK]
Hint: Use layout for structure, partials for reusable parts [OK]
Common Mistakes:
  • Duplicating header/footer in every page instead of partials
  • Skipping layout causing inconsistent page structure
  • Mixing content and layout roles improperly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard