Templating engines are tools that help developers build dynamic HTML pages. They allow you to inject data into templates and generate HTML content dynamically. Express.js supports various templating engines, making it easier to create server-rendered views. In this chapter, we'll explore different templating engines, how to integrate them with Express.js, and best practices for using them.
Bonus : Practical example in the end💡
Templating Engine?
A templating engineallows you to create HTML templates with embedded code. This code is replaced with data when the template is rendered, allowing for dynamic content generation. Templating engines help separate the presentation layer from the business logic, making your code cleaner and easier to maintain.
Popular Templating Engines
Several templating engines are commonly used with Express.js:
EJS (Embedded JavaScript): Simple and powerful, with JavaScript-like syntax.
Pug (formerly Jade): Minimalist syntax and indentation-based, making it very concise.
Handlebars: Logic-less templates with a focus on simplicity and readability.
Setting Up Templating Engines in Express.js
To use a templating engine in Express.js, you need to set the view engine and configure the views directory.
Example: Basic Setup
const express = require('express');
const app = express();
// Set the view engine
app.set('view engine', 'ejs');
// Set the views directory
app.set('views', './views');
// Define a route
app.get('/', (req, res) => {
res.render('index', { title: 'Welcome', message: 'Hello, World!' });
});
// Start the server
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
In this example, we set EJS as the view engine and define the directory where our views will be stored.
Using EJS (Embedded JavaScript)
EJS is a simple templating engine that lets you generate HTML with plain JavaScript.
npm install ejs
Creating Views
Create a directory named views and add an index.ejs file:
// views/index.pug
doctype html
html(lang="en")
head
meta(charset="UTF-8")
meta(name="viewport" content="width=device-width, initial-scale=1.0")
title= title
body
h1= message
Explanation:
Pug uses indentation to define HTML structure, making it concise and readable.
Output:
Navigating to http://localhost:3000/ will display a page with “Welcome” as the title and “Hello, World!” as the message.
Using Handlebars
Setting Up Handlebars
Handlebars is a logic-less templating engine, known for its simplicity and readability.
The server.js file sets up the server, defines routes, and renders views using EJS.
The main.ejs layout provides a common structure for all pages.
The header.ejs partial contains the header section of the pages.
The index.ejs template lists all blog posts.
The post.ejs template displays a single blog post.
Output:
Navigating to http://localhost:3000/ displays a list of blog posts.
Clicking on a post title navigates to the post’s page, displaying the post content.
Templating engines are a powerful tool for creating dynamic web pages in Express.js. By using templating engines like EJS, Pug, and Handlebars, you can separate your presentation layer from your business logic, making your code cleaner and more maintainable. This chapter covered the basics and advanced usage of templating engines, providing practical examples and best practices. By mastering templating engines, you can build robust and dynamic web applications with Express.js.Happy coding !❤️