Bird
Raised Fist0
Expressframework~20 mins

Template engine concept in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Template Engine Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the rendered output of this Express template?
Given the following Pug template and Express route, what will be the HTML output sent to the browser?
Express
app.get('/greet', (req, res) => {
  res.render('greet', { name: 'Alice' });
});

// Pug template 'greet.pug':
// h1 Hello #{name}!
// p Welcome to our site.
A<h1>Hello #{name}!</h1><p>Welcome to our site.</p>
B<h1>Hello !</h1><p>Welcome to our site.</p>
C<h1>Hello Alice!</h1><p>Welcome to our site.</p>
D<h1>Hello Alice</h1><p>Welcome to our site.</p>
Attempts:
2 left
💡 Hint
Remember that Pug replaces #{variable} with the value passed in render locals.
📝 Syntax
intermediate
2:00remaining
Which option correctly sets EJS as the template engine in Express?
Choose the correct code snippet to configure Express to use EJS templates located in the 'views' folder.
A
app.set('view engine', 'ejs');
app.set('views', './views');
B
app.engine('ejs', require('ejs').renderFile);
app.set('views', './templates');
C
app.set('view engine', 'ejs');
app.set('views', './templates');
D
app.engine('html', require('ejs').renderFile);
app.set('views', './views');
Attempts:
2 left
💡 Hint
The default views folder is './views' and setting 'view engine' to 'ejs' enables EJS.
🔧 Debug
advanced
2:00remaining
Why does this Handlebars template render '{{name}}' literally instead of the value?
Express route: app.get('/user', (req, res) => { res.render('profile', { username: 'Bob' }); }); Handlebars template 'profile.hbs':

User: {{name}}

ABecause the template file extension should be '.handlebars' not '.hbs'.
BBecause Handlebars requires triple braces {{{name}}} to render variables.
CBecause Express does not support Handlebars by default and needs extra setup.
DBecause the template uses {{name}} but the data key is 'username', so 'name' is undefined.
Attempts:
2 left
💡 Hint
Check if the variable names in the template match the keys in the data object.
❓ state_output
advanced
2:00remaining
What is the output of this EJS template with conditional rendering?
Express route: app.get('/status', (req, res) => { res.render('status', { loggedIn: false }); }); EJS template 'status.ejs': <% if (loggedIn) { %>

Welcome back!

<% } else { %>

Please log in.

<% } %>
A<p>Welcome back!</p>
B<p>Please log in.</p>
C<p><% if (loggedIn) { %>Welcome back!<% } else { %>Please log in.<% } %></p>
DSyntaxError due to missing closing tags
Attempts:
2 left
💡 Hint
Look at the value of 'loggedIn' and which block runs in the EJS template.
🧠 Conceptual
expert
2:00remaining
Which statement best describes how template engines improve Express app development?
Select the most accurate explanation of the role of template engines in Express.
AThey allow embedding JavaScript code directly in HTML files to generate dynamic content on the server before sending to the client.
BThey convert client-side JavaScript into server-side code to improve performance.
CThey automatically cache all static files to speed up page loading without server processing.
DThey replace Express middleware to handle HTTP requests more efficiently.
Attempts:
2 left
💡 Hint
Think about how templates combine data and HTML on the server.

Practice

(1/5)
1. What is the main purpose of using a template engine in Express?
easy
A. To improve server performance by caching files
B. To combine HTML with data and create dynamic web pages
C. To handle database connections automatically
D. To replace JavaScript code in the backend

Solution

  1. Step 1: Understand what template engines do

    Template engines allow mixing HTML with data to generate dynamic content.
  2. Step 2: Identify the main purpose in Express

    Express uses template engines to render HTML pages with data, making pages dynamic.
  3. Final Answer:

    To combine HTML with data and create dynamic web pages -> Option B
  4. Quick Check:

    Template engine purpose = dynamic HTML generation [OK]
Hint: Template engines create HTML pages with data dynamically [OK]
Common Mistakes:
  • Thinking template engines handle databases
  • Confusing template engines with performance tools
  • Believing template engines replace backend code
2. Which Express method is used to render a template with data?
easy
A. res.render()
B. res.sendFile()
C. res.json()
D. res.redirect()

Solution

  1. Step 1: Recall Express response methods

    Express has methods like res.sendFile, res.json, res.redirect, and res.render.
  2. Step 2: Identify the method for templates

    res.render() is specifically used to render templates with data.
  3. Final Answer:

    res.render() -> Option A
  4. Quick Check:

    Render templates = res.render() [OK]
Hint: Use res.render() to send templates with data [OK]
Common Mistakes:
  • Using res.sendFile() for templates
  • Confusing res.json() with rendering HTML
  • Trying to use res.redirect() to show templates
3. Given this Express code snippet, what will be the rendered output if the template shows <h1>Hello, <%= name %></h1> and res.render('greet', { name: 'Sam' }) is called?
medium
A.

Hello,

B.

Hello, <%= name %>

C.

Hello, Sam

D. Error: Template variable not found

Solution

  1. Step 1: Understand template variable usage

    The template uses <%= name %> to insert the value of 'name'.
  2. Step 2: Check the data passed to the template

    The code passes { name: 'Sam' }, so 'Sam' replaces <%= name %>.
  3. Final Answer:

    <h1>Hello, Sam</h1> -> Option C
  4. Quick Check:

    Template variable replaced by 'Sam' [OK]
Hint: Variables in templates show passed data values [OK]
Common Mistakes:
  • Thinking template tags show as text
  • Assuming missing variables cause errors
  • Ignoring passed data object
4. What is the error in this Express code snippet for rendering a template?
app.get('/hello', (req, res) => {
  res.render('hello', name: 'Alex');
});
medium
A. Template name should be a variable, not a string
B. The route path '/hello' is invalid
C. res.render() cannot be used inside app.get()
D. Missing curly braces around data object in res.render()

Solution

  1. Step 1: Check res.render() syntax

    res.render() expects the template name and a data object in curly braces.
  2. Step 2: Identify the mistake in data argument

    The code passes name: 'Alex' without curly braces, which is invalid syntax.
  3. Final Answer:

    Missing curly braces around data object in res.render() -> Option D
  4. Quick Check:

    Data object must be wrapped in {} [OK]
Hint: Always wrap data in {} when passing to res.render() [OK]
Common Mistakes:
  • Omitting curly braces for data object
  • Thinking template name must be variable
  • Believing res.render() can't be used in routes
5. You want to create a page that shows a list of users using a template engine in Express. Which approach correctly passes the user list to the template for rendering?
hard
A. res.render('users', { users: ['Anna', 'Ben', 'Cara'] });
B. res.render('users', ['Anna', 'Ben', 'Cara']);
C. res.render('users', users = ['Anna', 'Ben', 'Cara']);
D. res.render('users', { 'Anna', 'Ben', 'Cara' });

Solution

  1. Step 1: Understand how to pass data to templates

    Data must be passed as an object with keys matching template variables.
  2. Step 2: Check each option's syntax

    res.render('users', { users: ['Anna', 'Ben', 'Cara'] }); correctly passes an object with key 'users' and an array value.
  3. Final Answer:

    res.render('users', { users: ['Anna', 'Ben', 'Cara'] }); -> Option A
  4. Quick Check:

    Pass data as object with keys [OK]
Hint: Pass data as an object with named keys to res.render() [OK]
Common Mistakes:
  • Passing array directly without object
  • Using assignment inside res.render()
  • Using invalid object syntax
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