How To Convert From .ejs Template to React?
Last Updated :
09 Jul, 2024
Converting from Embedded JavaScript templates (.ejs) to React can significantly enhance the flexibility and interactivity of your web applications. React’s component-based architecture provides a more dynamic way to build user interfaces compared to traditional templating engines like EJS. This article will walk you through the process of converting .ejs templates to React components.
Steps for Creating React Applications:
Initialize a new React project
npx create-react-app my-app
Running the Application:
npm start
Approach 1: Manual Conversion
In the manual conversion process, we will transform server side EJS template to client side React components.
This steps involves finding EJS templates, creating corresponding react components and then convert EJS syntax to React code. You require deep understanding of EJS and React to translate server-side rendered templates into client-side components.
- Identify EJS Templates: Locate all your .ejs files.
- Create React Components: Create a new React component for each EJS template.
- Replace EJS Syntax: Convert EJS syntax (e.g., <%= data %>) to React syntax ({data}).
- Handle Logic: Replace EJS control structures with React state and lifecycle methods.
- Test Components: Ensure each component works as expected in isolation.
Dependencies
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Project Structure
project structureExample: In this example, we will be displaying Dynamic data in React.
JavaScript
// React Component (App.js):
import "./App.css";
function App() {
const title = "My App Title";
const user = { name: " Hello, GeekForGeeks!" };
const items = ["Javascript", "React", "NodeJS"];
return (
<div>
<head>
<title>{title}</title>
</head>
<body>
<h1>Hello, {user.name}!</h1>
<ul>
<li>{ items[0] }</li>
<li>{ items[1] }</li>
<li>{ items[2] }</li>
</ul>
</body>
</div>
);
}
export default App;
JavaScript
// Original EJS Template (index.ejs):
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
</head>
<body>
<h1>Hello, <%= user.name %>!</h1>
<ul>
<li><%= items[0] %></li>
<li><%= items[1] %></li>
<li><%= items[2] %></li>
</ul>
</body>
</html>
Code Explatation
- In the EJS template ('index.ejs'), we have created a HTML template to display dynamic data.
- In the 'app.js', we achieve the same functionality by defining the data as js variables . The react component then returns JSX that use curly braces to embed this variables into HTML structure.
- We have created 3 variables title, name & items with values. Then we have rendered the data in the React components in app.js
Output
Convert From .ejs Template to ReactUsing ChatGPT to Convert from EJS to React
In this Guide, we will use AI tools like ChatGPT to convert from EJS server side template to React client side rendering.
Using ChatGPT or any other AI tools to convert EJS to React is an quick way to convert from server-side template to client-side rendering.
- Extract the HTML structure and dynamic content from your EJS template.
- Request ChatGPT to convert the EJS template to React, specifying how dynamic content should translate into props and state.
- Review and integrate the converted JSX, ensuring it aligns with React syntax.
- Replace EJS logic with React state management and JSX rendering.
Folder Structure:
Folder StructureChatGPT: Open openai.chatgpt.com and enter prompt with JSX code.
Example: In this example, we will illustrate how to display a list of items in our application using React.
Code Explanation:
- The App component in App.js is a functional component that receives title, user, and items as props. The component efficiently renders a list (<ul>) of items using items.map() to iterate through the items array.
- In index.js we have created title, user, items and assigned value and send data to app.js
JavaScript
// App.js
import React from 'react';
const App = ({ title, user, items }) => {
return (
<html>
<head>
<title>{title}</title>
</head>
<body>
<h1>Hello, { user.name }!</h1>
<ul>
{ items.map((item, index) => (
<li key={index}>{item}</li>
))
}
</ul>
</body>
</html>
);
};
export default App;
JavaScript
// index.js
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
const data = {
title: "My App Title",
user: { name: "Hello, GeekForGeeks!" },
items: [ "Javascript", "React", "NodeJS" ],
};
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<App
title={ data.title }
user= { data.user }
items={ data.items }
/>
</React.StrictMode>
);
JavaScript
// Original EJS Template (index.ejs):
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
</head>
<body>
<h1>Hello, <%= user.name %>!</h1>
<ul>
<li><%= items[0] %></li>
<li><%= items[1] %></li>
<li><%= items[2] %></li>
</ul>
</body>
</html>
Output:
Convert From .ejs Template to React
Similar Reads
How To Convert Figma Design To React JS
Translating a design from Figma to a functional React JS application is a crucial step in the web development process. Figma provides powerful tools for designing user interfaces, and React JS offers a robust framework for building interactive and dynamic web applications. This article will explore
4 min read
How to Migrate from Create React App to Next JS ?
Migrating from Create React App to Next.js is a structured process that includes setting up a Next.js project, reorganizing your files, updating dependencies, and utilizing Next.js features for better performance and server-side rendering. Prerequisites:NPM & Node.jsReact JSNext JSApproach To mi
2 min read
How to Convert CSV to JSON in ReactJS ?
Dealing with data in various formats is a common task in web development. CSV (Comma Separated Values) and JSON (JavaScript Object Notation) are two widely used formats for storing and transmitting data. Converting CSV to JSON is often required when working with data in ReactJS applications. Approac
4 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 Connect Django with Reactjs ?
Connecting Django with React is a common approach for building full-stack applications. Django is used to manage the backend, database, APIs and React handles the User Interface on frontend. Prerequisites:A development machine with any OS (Linux/Windows/Mac).Python 3 installed.Node.js installed (ver
9 min read
How to use Handlebars to Render HTML Templates ?
Handlebars is a powerful template engine that allows us to create dynamic HTML templates. It helps to render HTML templates easily. One of the advantages of Handlebars is its simplicity, flexibility, and compatibility with various web development frameworks. These are the following methods: Table of
5 min read
How to use ReactJS with HTML Template ?
We all know ReactJS is a front-end development framework. For the front end, we need to create HTML templates. So, this article teaches you how to use HTML templates in ReactJS. The process is very simple. Prerequisites:NodeJS or NPMReact JSApproach: We will create a simple one-page HTML template i
4 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 is React Router is different from conventional routing ?
React Router is a library for routing in React applications. It allows you to create routes and link to them from your components. When a user clicks a link, React Router will determine which route to use and render the corresponding component. Prerequisites:NodeJS or NPMReact JSReact RouterSteps to
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