Full Post Page
 

Nodejs template engines with Express

Category: NODE JS & Written by Admin On April-10-2025 19:15:11

EXPRESSJS EJS Template official webiste ejs.co Embedded javaScript Templating-> EJS Template -> Template engine Based on javaScript EJS provides a clean and convenient way to create view in expessjs BENEFITS:create Dynamic and Reuable Template Ejs Template Tag syntax TAG description <% %> controll flow, no output <% = %> Output escaped value (safe) <% - % > Output unescaped value(unsafe) <% # %> comment not showing output <%-%> Remove the folling newline <%_%> remove white space before IT <%_%> remove all white space after IT benifit Prevents(save) for XSS attacks Example <ul> <% for(let i=1;i<=3;i++){%> <li> item <% = i %> <li> <% } %> </ul> create new project npm install -y npm install express nodemon ejs note fist nodejs,exprss nodemon for contine server run and ejs template engine GO package.json file scripting run developing "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start":"nodemon index.js" }, if you want to javaScript ES6 then Go package.json after"main": "index.js", write "type":"module", THAN create index.js file and write inclue const expess =require('expess') or ES6 import express from 'express'; RUN npm run start variable value print res.render("about" ,{title:'About page',message:'wekcome to EJS'}) <title> <%= title %> </title> javaScript control print loop ,condition <% if(message){%> <p><%= message %> </p> <%}else{ %> <span>Ther is no message</span> <% } %> loop <% let items = ["apple","Banana","orange","Cherry"] %> <ul> <% items.forEach( item => { %> <li> <%= item %> </li> <% }) %> </ul> // indexing array // array object app.get('/about',(req,res)=>{ var users=[ {name:'hotdr kumar',age:45,city:'mumbai'}, {name:'akshsy kumar',age:34,city:'patna'}, {name:'tsrd kumar',age:65,city:'ghar'}, {name:'tsmrd kumar',age:54,city:'hforla'}, ]; res.render("about",{ title:"Home page", message:"welcome", items:users }) }) //view template <table border="1" width="500px"> <% items.forEach( item => { %> <tr> <td> <%= item.name _%></td> <td> <%= item.age _%></td> <td> <%= item.city _%></td> </tr> <% }) %> </table> @@@@@@@@@@@@ EJS Template From Submission Partials Serving Static Files(css,js,Images) app.use(express.urlencoded('extended:false')) // use for form submited in index page app.get('/form',(req,res)=>{ res.render('form') }) app.post('/submit',(req,res)=>{ const name=req.body.myname const message=`Hello,${name} You submitted the form` res.send(message) }) EJS TEMPLATE :Partials header,menu,sidebar reusbility method <%- include ('header')-%> method <%- include ('footer')-%> app.use(express.static('public')) // use for css index.js

Comments

Share your thoughts about this post

Why Choose Us:

Experienced Instructors: Our team of experienced instructors are passionate about coding and dedicated to helping you succeed. With years of industry experience and a knack for teaching, they'll guide you every step of the way. Beginner-Friendly Content: Don't worry if you're new to coding. Our tutorials are designed with beginners in mind, starting from the fundamentals and gradually building up to more advanced topics. Updated Content: Coding is a rapidly evolving field, and we're committed to keeping our content up-to-date with the latest trends, technologies, and best practices. Community Support: Learning to code is more fun and effective when you're part of a supportive community. Join our online community forums to connect with fellow learners, share insights, and collaborate on projects. Get Started Today:



Recent Posts

Advance Js Fetch API Part-5

June-06-2025 21:23:08


Advance Js Promise Part-4

June-06-2025 11:35:05


Advance js oop part-3

June-06-2025 08:25:42


ExpressJs API With Image Crud-3

June-02-2025 21:09:41


NodeJs API-MVC-2

June-02-2025 10:56:22


ExpressJs API Crud-1

June-02-2025 10:27:58


ExpressJs Files upload

June-02-2025 10:15:40


NodeJS Cookie and CsrfToken

May-30-2025 19:20:40


ExpressJS Session

May-30-2025 19:14:29


ExpressJS NodeJs Crud Part-2

May-27-2025 21:52:18


ExpressJS NodeJS Crud Part-1

May-27-2025 21:33:30


ExpressJS Authentication

May-26-2025 17:11:41


NodeJS ExpressJS Files Upload

May-26-2025 16:53:29



Angular Introduction

May-13-2025 21:02:32



Advance Js part-1

May-12-2025 15:41:29