📖 Scenario: You are building a simple web page using Express.js that shows a list of fruits and their colors. You want to pass this data from your server code to the template so it can display the list dynamically.
🎯 Goal: Create an Express app that sends a list of fruits with their colors to a template. The template will then show each fruit and its color on the web page.
📋 What You'll Learn
Create a data object with fruit names and their colors
Set up a configuration variable for the page title
Pass the data and title to the template using res.render
Add the template code to display the fruit list and title
💡 Why This Matters
🌍 Real World
Passing data to templates is how web servers send dynamic content to users, like showing product lists or user profiles.
💼 Career
Understanding how to pass data to templates is essential for backend web developers working with Express and similar frameworks.
Progress0 / 4 steps
1
Create the fruit data object
Create a constant called fruits that is an array of objects. Each object should have name and color properties with these exact entries: { name: 'Apple', color: 'Red' }, { name: 'Banana', color: 'Yellow' }, { name: 'Grape', color: 'Purple' }.
Express
Hint
Use const fruits = [ ... ] with objects inside the array.
2
Add a page title variable
Create a constant called pageTitle and set it to the string 'Fruit Colors'.
Express
Hint
Use const pageTitle = 'Fruit Colors';
3
Pass data to the template in the route
Inside your Express app, create a GET route for '/'. Use res.render to render the template named 'fruits' and pass an object with fruits and pageTitle as properties.
In your template file named fruits.ejs, add an <h1> tag that shows the pageTitle. Then add an unordered list <ul> that loops over fruits and displays each fruit's name and color inside a <li>.
Express
Hint
Use EJS syntax: <% fruits.forEach(fruit => { %> and <%= fruit.name %>
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
Step 1: Understand res.render() usage
Express uses res.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 to res.render() -> Option D
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
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.
Step 2: Validate object syntax
Passing { title: 'Home' } correctly sends the variable title with value 'Home'.
Final Answer:
res.render('index', { title: 'Home' }); -> Option B
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
The data must be passed as an object with keys matching template variables.
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'] });.
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).