Full-Stack Web Development with Vue.js and Node

(singke) #1
Building an Express Application Chapter 2

The render method has two parameters: the view that it wants to load and the variables
that it wants to pass to that view. In this example, Users is passed to the title variable. And


in users.jade in the views folder, we have:


block content
h1= title
p Welcome to #{title}

This renders that variable inside both the h1 tag and the p tag. This way, we can pass any


content that we want from controllers to views. Let's add a new variable called


description to the render method in the users.js controller:


module.exports.controller = (app) => {
// get homepage
app.get('/users', (req, res) => {
res.render('users', { title: 'Users', description: 'This is the
description of all the users' });
})
}

Also, let's make a place where this would be rendered in users.pug:


extends layout

block content
h1= title
p Welcome to #{title}
p #{description}

If we reload the browser, we'll get:

Free download pdf