A layout is a main template that defines the common structure of your web pages, like header and footer, so you don't repeat the same code in every page.
Click to reveal answer
beginner
What are partials in Express templates?
Partials are small reusable template pieces, like a navigation bar or footer, that you can include in multiple pages to keep your code DRY (Don't Repeat Yourself).
Click to reveal answer
beginner
How do you include a partial in an EJS template?
You use the syntax <%- include('partialName') %> to insert the partial template named 'partialName' into your current template.
Click to reveal answer
beginner
Why use layouts and partials in Express apps?
They help keep your templates organized, avoid repeating code, and make it easier to update common parts like headers or footers in one place.
Click to reveal answer
intermediate
How does Express know which layout to use when rendering a page?
You can set a default layout in your template engine setup or specify a layout file when rendering a view, so Express wraps your page content inside that layout.
Click to reveal answer
What is the main purpose of a layout in Express templates?
ATo define a common page structure for multiple views
BTo store user data
CTo handle server requests
DTo style the page with CSS
✗ Incorrect
Layouts provide a shared structure like headers and footers for multiple pages.
Which syntax is used to include a partial in an EJS template?
A<partial src='partialName' />
B{{> partialName }}
C{% include 'partialName' %}
D<%- include('partialName') %>
✗ Incorrect
EJS uses <%- include('partialName') %> to insert partial templates.
Why are partials useful in template design?
AThey replace CSS files
BThey improve server speed
CThey allow reusing small template parts across pages
DThey store user sessions
✗ Incorrect
Partials help reuse common template pieces like navbars or footers.
How can you specify a layout when rendering a view in Express?
ABy using a CSS file named layout.css
BBy passing a layout option in res.render
CBy naming the file layout.html
DBy setting a global variable in JavaScript
✗ Incorrect
You can specify the layout file by passing { layout: 'layoutName' } in res.render.
Which of these is NOT a benefit of using layouts and partials?
AAutomatically improving database queries
BMaking templates easier to maintain
CReducing repeated code
DOrganizing common page elements
✗ Incorrect
Layouts and partials help with templates, not database queries.
Explain how layouts and partials work together in an Express app to build web pages.
Think about how a website header and footer appear on many pages.
You got /4 concepts.
Describe the steps to include a navigation bar partial in an EJS template using Express.
Focus on file creation and the include syntax.
You got /4 concepts.
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
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 B
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
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 A
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:
<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
Step 1: Understand layout and body insertion
The layout uses <%- body %> to insert the rendered content of index.ejs unescaped.
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 C
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
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.
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.
Final Answer:
You forgot to use a layout engine or middleware that injects the body content -> Option A
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
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 D
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