0
0
Expressframework~20 mins

Template engine concept in Express - Practice Problems & Coding Challenges

Choose your learning style9 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.