Passing data to templates lets you show dynamic content on web pages. It helps you send information from your server to the page users see.
Passing data to templates in Express
Introduction
Syntax
Express
res.render('templateName', { key1: value1, key2: value2 })The first argument is the template file name without extension.
The second argument is an object with data you want to use inside the template.
Examples
title variable with value 'Welcome Home' to the 'home' template.Express
res.render('home', { title: 'Welcome Home' })
username and age to the 'profile' template for dynamic display.Express
res.render('profile', { username: 'Alice', age: 30 })
Express
res.render('error', { message: 'Page not found' })
Sample Program
This Express app sends a user object to the 'index' template. The template uses the data to greet the user by name and show their age.
Express
import express from 'express'; const app = express(); app.set('view engine', 'ejs'); app.get('/', (req, res) => { const user = { name: 'Sam', age: 25 }; res.render('index', { user }); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); }); // index.ejs template content: // <h1>Hello, <%= user.name %>!</h1> // <p>You are <%= user.age %> years old.</p>
Important Notes
Make sure your template engine (like EJS) is installed and set up correctly.
Data keys become variables inside the template for easy use.
Passing complex objects is allowed, not just simple strings or numbers.
Summary
Use res.render() to send data to templates in Express.
Pass an object with keys and values as the second argument.
Templates use these keys as variables to show dynamic content.
Practice
1. In Express, how do you pass data to a template when rendering it?
easy
Solution
Step 1: Understand
Express usesres.render()usageres.render()to render templates and can accept data as the second argument.Step 2: Passing data as an object
The data is passed as an object with keys representing variable names in the template.Final Answer:
By passing an object as the second argument tores.render()-> Option DQuick Check:
Data passed viares.render()object = By passing an object as the second argument tores.render()[OK]
Hint: Use
res.render(view, dataObject) to pass data [OK]Common Mistakes:
- Trying to set data on
reqinstead ofres.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
Solution
Step 1: Check
The first parameter is the template name as a string, the second is an object with key-value pairs.res.render()parametersStep 2: Validate object syntax
Passing{ title: 'Home' }correctly sends the variabletitlewith value'Home'.Final Answer:
res.render('index', { title: 'Home' }); -> Option BQuick 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:
What variables can the
app.get('/', (req, res) => {
res.render('home', { user: 'Alice', age: 30 });
});What variables can the
home template access?medium
Solution
Step 1: Identify data passed to template
The object passed tores.render()has keysuserandagewith values 'Alice' and 30.Step 2: Understand template variable availability
The template can access all keys in the object as variables.Final Answer:
Bothuserwith 'Alice' andagewith 30 -> Option AQuick Check:
All keys in object = Bothuserwith 'Alice' andagewith 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
Solution
Step 1: Check
The second argument must be an object, so data should be inside curly braces.res.render()syntaxStep 2: Identify missing braces
Here,user: 'Bob'is passed without braces, causing syntax error.Final Answer:
Missing curly braces around data object -> Option CQuick 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
Solution
Step 1: Identify correct object structure
The data must be passed as an object with keys matching template variables.Step 2: Check data types for variables
nameis a string andtasksis an array, both correctly passed in res.render('dashboard', { name: 'Eve', tasks: ['Buy milk', 'Call Bob'] });.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).Final Answer:
res.render('dashboard', { name: 'Eve', tasks: ['Buy milk', 'Call Bob'] }); -> Option AQuick 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
10 questions ~5 mineasy hard
