How to print a variable directly using EJS template engine?
Last Updated :
02 Aug, 2024
EJS (Embedded JavaScript) is a templating engine for NodeJS that enables dynamic content generation in web applications. To print a variable directly in an EJS template, you can use the <%= variable %> syntax. This syntax allows you to embed and display the value of a variable directly within the HTML content of the template. In this article, we will explore the detailed practical demonstration to print a variable directly using the EJS template engine.
Steps to print a variable directly using the EJS template engine
Step 1: Firstly, we will make the folder named root by using the below command in the VScode Terminal. After creation use the cd command to navigate to the newly created folder.
mkdir root
cd root
Step 2: Once the folder is been created, we will initialize NPM using the below command, this will give us the package.json file.
npm init -y
Step 3: Once the project is been initialized, we need to install Express and EJS dependencies in our project by using the below installation command of NPM.
npm i express ejs
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"express": "^4.18.2",
"ejs": "^3.1.9",
}
Example: Below is an example of printing a variable directly using EJS template engine.
HTML
<!DOCTYPE html>
<head>
<title>EJS Example</title>
</head>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<h3>Print Variable Directly</h3>
<form action="/greet" method="post">
<label for="name">Enter your name:</label>
<input type="text" id="name" name="name" required>
<button type="submit">Submit</button>
</form>
<% if (greeting) { %>
<p><%= greeting %></p>
<% } %>
</body>
</html>
JavaScript
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const app = express();
const port = 3000;
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.render('index', { greeting: '' });
});
app.post('/greet', (req, res) => {
const name = req.body.name;
const greeting = `Hello, ${name}!`;
res.render('index', { greeting });
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
To run the application, we need to start the server by using the below command.
node server.js
Output:
Similar Reads
How to pass Variable to inline JavaScript using EJS Template Engine? In the EJS templating engine, we can pass variables to inline JS within our HTML templates. We can use the '<%= variable %>' syntax, where we can embed server-side variables directly into the client-side scripts. In this article, we will explore the detailed process to pass a variable to inlin
2 min read
How to Install & Use EJS Template Engine ? EJS (Embedded JavaScript) is mainly a templating language for JS which allows dynamic content generation in web-based applications. This EJS Template allows us to embed the JS code directly in the HTML markup, which enables the integration of data and logic into the presentation layer. We can use EJ
3 min read
How to use array of objects in EJS Template Engine ? Arrays of objects are collections of JavaScript objects stored within an array. Each object within the array can contain multiple key-value pairs, making them versatile for organizing various types of data. For example, you might have an array of objects representing users, products, or posts in a s
3 min read
How to use conditional statements with EJS Template Engine ? Conditional statements in EJS templates allow you to dynamically control the content that is rendered based on specified conditions. Similar to JavaScript, EJS provides a syntax for using conditional logic within your HTML templates. In this article, we will see a practical example of using conditio
3 min read
How to display the JSON data in EJS Template Engine ? EJS (Embedded JavaScript) is a templating language that allows dynamic content generation in NodeJS applications. It allows the integration of JavaScript code within HTML, making it easier to display dynamic data. To render JSON data in an EJS template, we can use EJS syntax to loop through the data
2 min read