Exercise-18_Use_EJS
Exercise-18_Use_EJS
1. Introduction
When quickly creating Node applications, a fast way to template your application is
sometimes necessary.
In this exercise, you will apply EJS to an Express application, include repeatable parts of
your site, and pass data to the views.
2. Requirements
EJS is installed.
Basic knowledge of HTML.
Create a new file named data.json in your project directory with the following sample
content:
With all of the dependencies installed, let’s configure the application to use EJS and set
up the routes for the Index page and the About page.
Create a new server.js file and open it with your code editor and add the following lines
of code:
// server.js
var express = require('express');
var app = express();
// set the view engine to ejs
app.set('view engine', 'ejs');
// use res.render to load up an ejs view file
// index page
app.get('/', function (req, res) {
res.render('pages/index');
});
// about page
app.get('/about', function (req, res) {
res.render('pages/about');
});
const PORT = process.env.PORT || 3000;
Page 1
app.listen(PORT, () => console.log(`Server running on http://localhost:$
{PORT}`));
Like a lot of the applications you build, there will be a lot of code that is reused. These
are considered partials. In this example, there will be three partials that will be reused
on the Index page and About page: head.ejs, header.ejs, and footer.ejs. Let’s make
those files now.
Page 2
views/partials/head.ejs
views/partials/header.ejs
views/partials/footer.ejs
Page 3
Next, you will use these partials in index.ejs and about.ejs.
You have three partials defined. Now you can include them in your views.
The hyphen <%- instead of just <% to tell EJS to render raw HTML.
The path to the partial is relative to the current file.
views/pages/index.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<%- include('../partials/head'); %>
</head>
<body class="container">
<header><%- include('../partials/header'); %></header>
<main>
<div class="jumbotron">
<h1>This is great</h1>
<p>Welcome to templating using EJS</p>
</div>
</main>
Next, create a new about.ejs file and open it with your code editor. Add the following
lines of code:
views/pages/about.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<%- include('../partials/head'); %>
</head>
<body class="container">
Page 4
<header><%- include('../partials/header')%></header>
<main>
<div class="row">
<div class="col-sm-8">
<div class="jumbotron">
<h1>This is great</h1>
<p>Welcome to templating using EJS</p>
</div>
</div>
<div class="col-sm-4">
<div class="well">
<h3>Look I'm A Sidebar!</h3>
</div>
</div>
</div>
</main>
Save the changes to this file and then run the application:
node server.js
Let’s define some basic variables and a list to pass to the Index page.
Revisit server.js in your code editor and add the following lines of code inside the
app.get('/') route:
// server.js
var express = require('express');
var app = express();
// set the view engine to ejs
app.set('view engine', 'ejs');
// use res.render to load up an ejs view file
// index page
app.get('/', function (req, res) {
var mascots = [
Page 5
{ name: 'Sammy', organization: "DigitalOcean", birth_year: 2012},
{ name: 'Tux', organization: "Linux", birth_year: 1996},
{ name: 'Moby Dock', organization: "Docker", birth_year: 2013}
];
var tagline = "No programming concept is complete without a cute
animal mascot.";
res.render('pages/index', {
mascots: mascots,
tagline: tagline
});
});
// about page
app.get('/about', function (req, res) {
res.render('pages/about');
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on http://localhost:$
{PORT}`));
This code defines an array called mascots and a string called tagline. Next, let’s use them
in index.ejs.
Revisit index.ejs in your code editor and add the following lines of code:
views/pages/index.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<%- include('../partials/head'); %>
</head>
<body class="container">
<header><%- include('../partials/header'); %></header>
<main>
<div class="jumbotron">
<h1>This is great</h1>
<p>Welcome to templating using EJS</p>
<h2>Variable</h2>
<p><%= tagline %></p>
Page 6
</div>
</main>
Revisit index.ejs in your code editor and add the following lines of code:
views/pages/index.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<%- include('../partials/head'); %>
</head>
<body class="container">
<header><%- include('../partials/header'); %></header>
<main>
<div class="jumbotron">
<h1>This is great</h1>
<p>Welcome to templating using EJS</p>
<h2>Variable</h2>
<p><%= tagline %></p>
<ul>
<% mascots.forEach(function(mascot) { %>
<li>
<strong><%= mascot.name %></strong>
representing <%= mascot.organization %>,
born <%= mascot.birth_year %>
</li>
<% }); %>
</ul>
</div>
</main>
Page 7
Passing Data to a Partial in EJS
The EJS partial has access to all the same data as the parent view. But be careful. If you
are referencing a variable in a partial, it needs to be defined in every view that uses the
partial or it will throw an error.
You can also define and pass variables to an EJS partial in the include syntax like this:
views/pages/about.ejs
If you want to reference a variable in a partial that may not always be defined, and give
it a default value, you can do so like this:
views/partials/header.ejs
If you visit http://localhost:3000/ in a web browser, you can observe the Index page
with the mascots:
Page 8
Page 9