DEV Community

Rittwick Bhabak
Rittwick Bhabak

Posted on

Node.js Crash Course - Part 7 - View Engines

By using view engine, we can inject dynamic content in the html.
In this tutorial we are going to use ejs view engine.

1. Installing ejs

npm install ejs will install the ejs package.

2. Registering ejs

ejs have to be registered after we create the express app.

const app = express();
app.set('view engine', 'ejs');
Enter fullscreen mode Exit fullscreen mode

By default the templates will be searched in /views folder. We can change the setting explicitly.

const app = express();
app.set('view engine', 'ejs');
app.set('views', 'myviews');
Enter fullscreen mode Exit fullscreen mode

Then templates will be searched in '/myviews' folder.

3. Rendering HTML Pages

Instead of res.sendFile() we are going to use app.render()

app.get('/', (req, res) => {
    res.render('index');
    // AND NOT res.sendFile('./views/index.html', ...);
})
Enter fullscreen mode Exit fullscreen mode

res.render('index'): inside render we are going to place the filename without the extension.

4. Passing Data into views

app.get('/about', (req, res) => {
    res.render('about', { title: 'About' });
})
Enter fullscreen mode Exit fullscreen mode

Now, title will be accessible from templates in the following way:

<%= title %>
Enter fullscreen mode Exit fullscreen mode

if-else in ejs

<% if (blogs.length > 0) { %> 
        <h3><%= blogs.length %> posts to display %>
<% } else { %>
        <h3>No Blog Posts to display...</h3>
 <% } %>
Enter fullscreen mode Exit fullscreen mode

for loop in ejs

<% blogs.forEach(blog => { %> 
<%= blog.title %>
<% }) %>
Enter fullscreen mode Exit fullscreen mode

To print data <%= data %> is used and to write logic <% logic %> is used.

A shortcut

In app.js

app.get('/', (req, res) => {
    res.render('index', { numbers: numbers });
})
Enter fullscreen mode Exit fullscreen mode

Instead of this,

app.get('/', (req, res) => {
    res.render('index', { numbers });
})
Enter fullscreen mode Exit fullscreen mode

Instead of {numbers: numbers}, we can use { numbers }.

5. Partial

Assume the following ejs file

<html>
<head>
    A long header
</head>
<body>
    <nav> 
        A complicated navbar
    </nav>
    <div>
       ...
       content of page 1
       ...
    </div>
    <footer>
        common footer
    </footer>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Now, suppose we have 50 such pages.

We can cut the nav, head, and footer as they are common in every page and place them nav.ejs, head.ejs and footer.ejs file respectively

<!--nav.ejs-->
<nav> 
    A complicated navbar
</nav>


<!--head.ejs-->
<head>
    A long header
</head>


<!--footer.ejs-->
<footer>
    common footer
</footer>
Enter fullscreen mode Exit fullscreen mode

Then the structure of the 50 pages will be reduced to:

<html>
<%- include('./partials/head.ejs') %>
<body>
    <%- include('./partials/nav.ejs') %>
    <div>
        ...
        content goes here
        ...
    </div>
    <%- include('./partials/footer.ejs') %>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)