Steps to Create an EJS application

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

<%= vs <%- in EJS Template Engine

EJS stands for Embeded JavaScript. It’s a simple templating language that lets you generate HTML with plain JavaScript. EJS allows you to embed JavaScript code directly into your HTML. It is often used directly in your HTML markup, making it easy to generate dynamic content.

In this article, we will discuss <%= and <%- in EJS, along with discussing significant differences that distinct them from each other.

Similar Reads

What is <%=?

‘<%= %>’ is used to give out the output value of JavaScript expression into the HTML markup language. They directly output the result of the JavaScript expression and escape the HTML entity to prevent XSS(Cross-site-scripting) attacks....

Steps to Create an EJS application:

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....

Project Structure:

...

What is <%-?

‘<%- %>’ is majorly used for unescaped output, it directly injects the raw value of a javaScript expression into the HTML markup. It is useful for rendering HTML content generating the user input....

Difference between <%= and <%- in EJS:

Features <%= %> <%- %> Output Escapes HTML entities Does not escapes HTML entities HTML escape Yes No Use Case Safer for user input Rendering raw HTML from user input. When to use Rendering plain text Rendering HTML content Security consdieraaation Safer, prevents XSS attacks Requires necessary caution to avoid XSS inssues. Syntax <%= username %> <%- username %>...

Conclusion:

Understanding difference between these synatax is crucial for developers when working with EJS, as it maintains proper handling of data. These tags in EJS are important to determine how data is rendered on WEB PAGE. <%= %> is used for direct general output, maintain the safer rendering environment, while <%- %> is used when raw HTML output is required. Understanding the use case of these tag is important to maintain secure web development while using EJS....