Bird
Raised Fist0
Expressframework~20 mins

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

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

Practice

(1/5)
1. In Express, how do you pass data to a template when rendering it?
easy
A. By modifying the template file before rendering
B. By setting variables directly on req object
C. By sending data in the URL query string
D. By passing an object as the second argument to res.render()

Solution

  1. Step 1: Understand res.render() usage

    Express uses res.render() to render templates and can accept data as the second argument.
  2. Step 2: Passing data as an object

    The data is passed as an object with keys representing variable names in the template.
  3. Final Answer:

    By passing an object as the second argument to res.render() -> Option D
  4. Quick Check:

    Data passed via res.render() object = By passing an object as the second argument to res.render() [OK]
Hint: Use res.render(view, dataObject) to pass data [OK]
Common Mistakes:
  • Trying to set data on req instead of res.render()
  • Passing data as a string instead of an object
  • Modifying template files directly for dynamic data
2. Which of the following is the correct syntax to pass a variable named title with value 'Home' to a template named index?
easy
A. res.render('index', ['title', 'Home']);
B. res.render('index', { title: 'Home' });
C. res.render('index', title = 'Home');
D. res.render('index', 'title', 'Home');

Solution

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

    The first parameter is the template name as a string, the second is an object with key-value pairs.
  2. Step 2: Validate object syntax

    Passing { title: 'Home' } correctly sends the variable title with value 'Home'.
  3. Final Answer:

    res.render('index', { title: 'Home' }); -> Option B
  4. Quick Check:

    Object with key-value = res.render('index', { title: 'Home' }); [OK]
Hint: Pass data as an object literal in second argument [OK]
Common Mistakes:
  • Using assignment instead of object literal
  • Passing array instead of object
  • Passing multiple string arguments instead of one object
3. Given this Express code snippet:
app.get('/', (req, res) => {
  res.render('home', { user: 'Alice', age: 30 });
});

What variables can the home template access?
medium
A. Both user with 'Alice' and age with 30
B. Only age with value 30
C. Only user with value 'Alice'
D. No variables, template receives no data

Solution

  1. Step 1: Identify data passed to template

    The object passed to res.render() has keys user and age with values 'Alice' and 30.
  2. Step 2: Understand template variable availability

    The template can access all keys in the object as variables.
  3. Final Answer:

    Both user with 'Alice' and age with 30 -> Option A
  4. Quick Check:

    All keys in object = Both user with 'Alice' and age with 30 [OK]
Hint: All keys in object become template variables [OK]
Common Mistakes:
  • Assuming only one variable is passed
  • Thinking template cannot access numbers
  • Believing data must be nested inside another object
4. What is wrong with this Express code snippet?
app.get('/profile', (req, res) => {
  res.render('profile', user: 'Bob');
});
medium
A. Callback function should have three parameters
B. Template name should be a variable, not string
C. Missing curly braces around data object
D. Cannot pass data to templates in Express

Solution

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

    The second argument must be an object, so data should be inside curly braces.
  2. Step 2: Identify missing braces

    Here, user: 'Bob' is passed without braces, causing syntax error.
  3. Final Answer:

    Missing curly braces around data object -> Option C
  4. Quick Check:

    Data must be an object literal = Missing curly braces around data object [OK]
Hint: Always wrap data in { } when passing to res.render() [OK]
Common Mistakes:
  • Passing key-value pairs without braces
  • Using variables instead of strings for template name
  • Adding extra parameters to callback unnecessarily
5. You want to render a template dashboard that shows a user's name and a list of their tasks. Which code correctly passes this data to the template?
hard
A. res.render('dashboard', { name: 'Eve', tasks: ['Buy milk', 'Call Bob'] });
B. res.render('dashboard', 'name', 'Eve', 'tasks', ['Buy milk', 'Call Bob']);
C. res.render('dashboard', { 'Eve', ['Buy milk', 'Call Bob'] });
D. res.render('dashboard', { user: 'Eve', taskList: 'Buy milk, Call Bob' });

Solution

  1. Step 1: Identify correct object structure

    The data must be passed as an object with keys matching template variables.
  2. Step 2: Check data types for variables

    name is a string and tasks is an array, both correctly passed in res.render('dashboard', { name: 'Eve', tasks: ['Buy milk', 'Call Bob'] });.
  3. Step 3: Evaluate other options

    The other options use incorrect syntax such as multiple separate arguments, invalid object literals without keys, or mismatched variable names and data types (string instead of array).
  4. Final Answer:

    res.render('dashboard', { name: 'Eve', tasks: ['Buy milk', 'Call Bob'] }); -> Option A
  5. Quick Check:

    Correct object with array value = res.render('dashboard', { name: 'Eve', tasks: ['Buy milk', 'Call Bob'] }); [OK]
Hint: Pass arrays inside object to send lists to templates [OK]
Common Mistakes:
  • Passing multiple arguments instead of one object
  • Using wrong data types for lists
  • Mismatching variable names expected by template
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