How To Render Dynamic Lists Using EJS and Express ? Last Updated : 09 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Rendering of dynamic lists in EJS and Express mainly allows us to dynamically generate the HTML content which is based on the data from the backend server. We can render dynamic lists using the '<% %>' tags.Steps to Render Dynamic ListsStep 1: Create a directory for the projectmkdir rootcd rootStep 2: Initialize the Node Applicationnpm init -yStep 3: Install the required dependenciesnpm i express ejsProject StructureCreate the required files and folder as shown hereDependencies"dependencies": { "express": "^4.18.2", "ejs": "^3.1.9",}Step 4: Setting up the Express AppFirst, create a file named app.js and initialize your Express application (if not done already) JavaScript //app.js const express = require('express'); const app = express(); const port = 3000; app.set('view engine', 'ejs'); app.use(express.static('public')); app.get('/', (req, res) => { res.render('index', { data: [], showButton: true }); }); app.get('/list', (req, res) => { const data = ['JavaScript', 'Node.js', 'Express', 'EJS']; res.render('index', { data: data, showButton: false }); }); app.listen(port, () => { console.log(`Server is running at http://localhost:${port}`); }); Set the view engine: app.set('view engine', 'ejs') specifies that we’re using EJS for templating.Serve static files: app.use(express.static('public')) allows serving static assets like CSS and images.Routes:/: Renders the page with an empty list and a button./list: Renders the page with a predefined list and hides the buttonStep 5: Creating the EJS TemplateCreate a file named index.ejs in the views folder with the following content: HTML //index.ejs <html> <head></head> <body> <h1 style="color: green;">GeeksforGeeks</h1> <h3>Rendering Dynamic List</h3> <% if (showButton) { %> <form action="/list" method="get"> <button type="submit">Show List</button> </form> <% } %> <ul> <% data.forEach(item=> { %> <li> <%= item %> </li> <% }); %> </ul> </body> </html> Conditional Rendering: <% if (showButton) { %>: Displays a button if showButton is true.Dynamic List Rendering: <% data.forEach(item => { %>: Iterates over the data array to create list items dynamically.Output Escaping: <%= item %>: Outputs the item safely to prevent XSS attacks.To run the application, we need to start the server by using the below command.node server.jsOutput Comment More infoAdvertise with us Next Article How To Render Dynamic Lists Using EJS and Express ? A anjalibo6rb0 Follow Improve Article Tags : Web Technologies Node.js Express.js EJS-Templating Language View Engine +1 More Similar Reads How to render JSON in EJS using Express.js ? EJS (Embedded JavaScript) is a templating language that allows dynamic content generation in NodeJS applications. It allows us to embed JavaScript code directly into Our HTML code, and with the help of that we can generate the dynamic content on the server side. So with the help of ejs, we can perfo 4 min read Pagination using Node, Mongo, Express.js and EJS In this article, we will dive into the process of implementing pagination in our web application using NodeJS, MongoDB, Express, and EJS for dynamic HTML rendering. Additionally, we'll enhance the visual appeal of our pagination controls with CSS. Our focus will be on creating a user profile system 4 min read How to use Template Engines in Express JS ? Express.js is a popular web framework for Node.js that simplifies the process of building web applications. One of the key features of Express is its ability to integrate with template engines, allowing developers to dynamically generate HTML pages with data from their server. In this article, we'll 3 min read How to do Templating using ExpressJS in Node.js ? Template Engine : A template engine basically helps us to use the static template files with minimal code. At runtime, the template engine replaces all the variables with actual values at the client-side. Templating Engine Examples: EJS (Embedded JavaScript Templating) Pug Mustache In this article w 2 min read How to Render a variable as HTML in EJS ? In the EJS, the rendering of a variable as HTML consists of a specific tage through which we can control how the content is displayed. This tag involves, <% code %> that allows the execution of code without rendering, <%= code %> escapes and prints the code as HTML, while <%- code % 2 min read Like