Welcome back, dear crafter! Today, we'll delve into the world of Template Engines using two popular ones: EJS (Embedded JavaScript Templates) and Pug (formerly known as Jade). Let's get started!
Template engines are tools that help to separate the HTML layout from the application's logic in a web development project. They enable developers to write HTML templates that can be dynamically rendered using JavaScript, making the process more efficient and maintainable.
First, let's create a new Node.js project:
mkdir my-template-project
cd my-template-project
npm init -yNow, to install EJS, run:
npm install ejsFor Pug, run:
npm install pugYou can install both at once using the following command:
npm install ejs pugCreate a new file named index.ejs in a views folder:
mkdir views
touch views/index.ejsLet's add a simple EJS template:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My EJS Project</title>
</head>
<body>
<h1>Welcome to My EJS Project!</h1>
</body>
</html>To render the EJS template, create an app.js file with the following content:
const express = require('express');
const app = express();
const ejs = require('ejs');
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.get('/', (req, res) => {
res.render('index', { message: 'Hello, EJS!' });
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});Now, start the server:
node app.jsVisit http://localhost:3000 in your browser to see the output.
You can access variables passed to the template using <%= variableName %>.
Create a new file named index.pug in the views folder:
mkdir views
touch views/index.pugLet's add a simple Pug template:
doctype html
html(lang="en")
head
meta(charset="UTF-8")
meta(name="viewport", content="width=device-width, initial-scale=1.0")
title My Pug Project
body
h1 Welcome to My Pug Project!To render the Pug template, update the app.js file:
const express = require('express');
const app = express();
const pug = require('pug');
app.engine('pug', pug.__express);
app.set('view engine', 'pug');
app.set('views', __dirname + '/views');
app.get('/', (req, res) => {
const viewHTML = pug.renderFile('./views/index.pug', { message: 'Hello, Pug!' });
res.send(viewHTML);
});
app.listen(3000, () => {
console.log('Server started on port 3000');
});Now, start the server:
node app.jsVisit http://localhost:3000 in your browser to see the output.
You can access variables passed to the template using = variableName =.
Both EJS and Pug have their unique features. EJS is more HTML-like and easier for beginners, while Pug is more concise and efficient, making it a popular choice among experienced developers.
What's the main purpose of using a Template Engine in a Node.js project?
Happy learning, and remember to keep coding! 🚀💻
This is just the beginning of our journey with Template Engines in Node.js. In the next lessons, we will dive deeper into the features of EJS and Pug, and explore how to use them in real-world projects. Stay tuned! 🎉🎓