Understanding EJS (Embedded JavaScript)
Introduction
Welcome, students! Today, we will delve into the world of EJS, also known as Embedded JavaScript. EJS is a popular template engine for Node.js and Express.js, used to generate HTML markup with plain JavaScript.
Part 1: What is EJS?
1.1 Definition
EJS is a simple templating language that lets you generate HTML markup with JavaScript. It allows you to embed JavaScript code directly within your HTML files. 1.2 Features
EJS supports both server-side and client-side template rendering. It provides a straightforward syntax for embedding dynamic content, including variables, control flow, and template inclusion. EJS enforces a clear separation between logic and presentation, facilitating code organization and reusability. 1.3 Example:
html<h1>Hello, <%= user.name %>!</h1>
In this example, <%= user.name %> is an EJS tag that injects the value of user.name into the HTML output.
Part 2: Integration with Node.js/Express.js
2.1 Setup
EJS can be easily integrated with Node.js and Express.js using the ejs package, which is commonly used for server-side rendering of templates. 2.2 Example Usage:
const express = require('express'); const app = express();
// Set 'ejs' as the view engine app.set('view engine', 'ejs');
// Render an EJS template
app.get('/', (req, res) => { const data = { user: { name: 'John' } };
res.render('index', { data }); });
In this example, we set EJS as the view engine for Express. When rendering the 'index' template, we pass a data object containing dynamic content to be injected into the template.
Part 3: Use Cases
3.1 Dynamic Content
EJS is ideal for rendering dynamic content, such as user-specific data, database records, or real-time information. 3.2 Reusable Components
It enables the creation of reusable template components, promoting modular design and code reusability.
In conclusion, EJS (Embedded JavaScript) serves as a powerful tool for generating HTML templates with dynamic content using JavaScript. Its seamless integration with Node.js and Express.js makes it a popular choice for server-side rendering in web applications.
By understanding EJS, students gain valuable insights into the world of server-side template rendering and dynamic content generation. This knowledge equips them with a versatile tool for building interactive and data-driven web applications using Node.js and Express.js.An example of what the content of the "index.ejs" file could look like. This file should be placed in a directory called "views" in the root directory of your Express application:
In our express.js program, the views directory is being referenced in the following line of code:
javascriptapp.set('view engine', 'ejs');
In this line, the app.set method is used to set the 'view engine' to 'ejs'. When Express is configured in this way, it automatically knows that the templates are stored in a directory named "views". This is a convention in Express.js where the views directory is assumed to be named "views" unless otherwise specified.
Therefore, when you provide the view engine as 'ejs', Express expects to find the EJS template files in the "views" directory. This is how Express knows where to look for the EJS templates when rendering them for a route.
Additionally, when rendering the EJS templates using res.render() method, Express will look for the specified template file within the "views" directory by default unless a specific path is provided.
So, the line app.set('view engine', 'ejs'); is where the views directory is referenced and configured as the default location for EJS templates in the Express application.
In this "index.ejs" file, we have a simple HTML form for entering two numbers and choosing an arithmetic operation. When the form is submitted, it makes a POST request to the "/calculate" route in the Express app. If there is a result available, it will be displayed below the form.
Make sure to save this code in a file named "index.ejs" and place it in a directory called "views" in the root directory of your Express application.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="/calculate" method="post">
<input type="number" name="num1" placeholder="Enter first number" required>
<select name="operation">
<option value="+" selected>Addition (+)</option>
<option value="-">Subtraction (-)</option>
<option value="*">Multiplication (*)</option>
<option value="/">Division (/)</option>
</select>
<input type="number" name="num2" placeholder="Enter second number" required>
<button type="submit">Calculate</button>
</form>
<% if (typeof result !== 'undefined') { %>
<h2>Result: <%= result %></h2>
<% } %>
</body>
</html>