0
0
Expressframework~20 mins

Passing data to templates in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Data Master
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 route with EJS template?

Given the Express route below and an EJS template that displays <%= message %>, what will the user see in the browser?

Express
app.get('/greet', (req, res) => {
  res.render('greet', { message: 'Hello, friend!' });
});
AThe page shows: <%= message %>
BThe page shows: message
CThe page is blank with no text
DThe page shows: Hello, friend!
Attempts:
2 left
💡 Hint

Remember that res.render passes data to the template, and EJS replaces <%= %> with the value.

📝 Syntax
intermediate
2:00remaining
Which option correctly passes multiple variables to a Pug template?

Choose the correct way to pass title and user variables to a Pug template in Express.

Express
app.get('/profile', (req, res) => {
  // pass variables here
});
Ares.render('profile', title = 'User Profile', user = req.user);
Bres.render('profile', { title: 'User Profile', user: req.user });
Cres.render('profile', ['title', 'User Profile', 'user', req.user]);
Dres.render('profile', { 'title': 'User Profile' }, { 'user': req.user });
Attempts:
2 left
💡 Hint

Remember that res.render takes the template name and a single object with all variables.

🔧 Debug
advanced
2:00remaining
Why does this Express route fail to pass data to the Handlebars template?

Examine the code below. The template does not receive the name variable. What is the cause?

Express
app.get('/hello', (req, res) => {
  const name = 'Alice';
  res.render('hello');
});
AThe <code>name</code> variable is not passed in the <code>res.render</code> call.
BThe variable <code>name</code> must be declared with <code>var</code> to be passed.
CThe route should use <code>res.send</code> instead of <code>res.render</code>.
DThe template engine does not support variables named <code>name</code>.
Attempts:
2 left
💡 Hint

Check how variables are passed to templates in Express.

state_output
advanced
2:00remaining
What is the output when passing nested data to a template?

Given this Express route and a Pug template that uses user.name and user.age, what will be rendered?

Express
app.get('/user', (req, res) => {
  res.render('user', { user: { name: 'Bob', age: 30 } });
});
AThe page shows: Name: Bob, Age: 30
BThe page shows: Name: undefined, Age: undefined
CThe page shows: Name: [object Object], Age: [object Object]
DThe page is blank with no user info
Attempts:
2 left
💡 Hint

Remember how nested objects are accessed in templates.

🧠 Conceptual
expert
3:00remaining
Which option best explains why data passed to templates must be plain objects?

Why should the data passed to Express templates be plain JavaScript objects rather than complex types like functions or class instances?

ATemplates require data to be JSON strings, so objects must be serialized first.
BExpress automatically converts all data to strings, so complex types are ignored.
CTemplates can only safely render simple data structures; complex types may cause errors or unexpected output.
DPassing functions or classes improves performance but is discouraged for security reasons.
Attempts:
2 left
💡 Hint

Think about how templates access and display data.