Skip to main content

Getting started with Ejs template engine

Getting started with Ejs template engine

 Hi. In this tutorial, I will show you how start using template engines in your node js/express js applications. Template engines are the easiest ways to get started with building your express js application.  I recommend you check out our video content and other useful resources on our YouTube channel and this blog respectively.

NB: Basic node js/express knowledge required for this tutorial

What is template engine?

In simple terms, a template engine is a package that renders data in HTML pages. It easily allows us to create and use static html page.

One particularity with template engines is their ability to inject data from the server into an html template and then send the final html page to the client side. In other words, a variable can be created in our server and then be inserted in an html template.

 We have several examples of template engines which are usable with node js and express js which include:

  1. Pug
  2. Handlebars 
  3. EJS
  4. Hogan etc

With simplicity in syntax and structure, template engines can be used as a quick tool to easily get started with a Node js application if you don’t want to do anything complex or involve other front-end frameworks like React JS or Vue JS.

In this tutorial, we will use discuss about EJS template engine.

Installation

To add EJS in your Express JS project, you must install the module in order to start using it. To install EJS module, navigate to your project directory from a command prompt window and type the command:

Getting started with Ejs template engine


After hitting enter, just wait for a while as npm downloads and installs it in your local machine. please take note this requires internet.

Usage

As example, we are going to setup a simple express app that renders a web page with variables using EJS.

To begin using EJS in our express app, we must import/call EJS and set view engine as EJS in our code. You will understand better when you see in code .

Finally, at the route handler, you just render the file using the method re.render() which takes as parameter the EJS file name (without extension) and the variable.

EJS files have the extension .ejs and contains html which has portions of server variables written in EJS syntax. This EJS file is stored in a folder called views where express checks by default for rendering web pages.

Take note of the fact that, though express js has the ability to respond to a request by sending a file, it can not really inject a variable into the html template just like EJS did.

Enough of the talking, let’s do the coding

Create a folder called views in your node js project directory. 

server.js

Getting started with Ejs template engine
  • Notice how a string value was assigned to variable title.
In the views folder, create an index.ejs file.

index.ejs

Getting started with Ejs template engine
  • Notice how I represented the variable in the HTML code.
  • EJS uses the syntax <%= … %> to represent variables in HTML code.

OUTPUT

Getting started with Ejs template engine

Thanks for coding with me.

Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...