02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Working with template systems - EJS

Working with Template Systems - EJS

In this tutorial, we will explore how to work with EJS, a templating system for Node.js. EJS, or Embedded JavaScript, is a simple and powerful tool for rendering HTML with dynamic content. In this lesson, we will cover the basics of installing and configuring EJS to set up dynamic views in your Node.js application.

1. What is EJS?

EJS (Embedded JavaScript) is a templating mechanism that allows you to dynamically generate HTML content within Node.js. By using EJS, we can create templates of HTML that the server will render and deliver to the user. It’s an easy and effective way to handle server-side rendering, especially when compared to other options. In this tutorial, we will learn how to install EJS, configure it within our Node.js server, and create a basic layout.

2. Installing EJS

To get started, let's install EJS using npm. Follow these steps:

# Install EJS and save it to the production dependencies
npm install ejs --save

If you encounter any issues, ensure that the command is correct and that you include all necessary letters.

3. Configuring EJS in Express

After installing EJS, we need to configure it in our server code so that Express knows to use EJS as the templating engine. In the main server file (e.g., server.js), add the following line after setting up your public folder:

// Set EJS as the templating engine for the app
app.set('view engine', 'ejs');

This line informs Express to use EJS for rendering views.

4. Creating the Views Folder

For EJS to function properly, you need to create a directory for your view templates. By default, Express will look for the folder named views within your root directory:

project-folder/
  views/
    layout/
      empty.ejs

Inside the views folder, we can create subdirectories, such as layout, where we store our layout files. In this tutorial, we will create an empty layout to understand how to pass data between the server and the view.

5. Using Layouts and Variables

To demonstrate how to pass variables into the EJS view, follow these steps:

  1. Create an EJS Layout File: Create a file named empty.ejs in the views/layout folder.
  2. Add an HTML Structure: The layout file should contain a simple HTML skeleton, with placeholder tags for dynamic content:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title><%= title %></title>
</head>
<body>
    <div><%= body %></div>
</body>
</html>
  1. Pass Variables from the Server: In our server.js, let’s pass some dynamic content to this view. Instead of using res.send(), use res.render() to render the EJS file and pass in the necessary data:
app.get('/register', (req, res) => {
    res.render('layout/empty', {
        title: 'Register',
        body: 'Welcome to the registration page!'
    });
});

Here, we are using the render() function to pass two variables, title and body, which will be used in the empty.ejs template.

6. Testing the Setup

After you have made these changes, you can run the server:

node server.js

Then, navigate to http://localhost:3001/register in your browser. You should see the HTML page dynamically populated with the title "Register" and a body message.

7. More Complex Layouts

You can also create more complex layouts that handle overlays and contain additional HTML elements. For example, create another layout named overlay.ejs and include more styling or JavaScript, just like you would for a React component. Note that with EJS, we’re dealing directly with HTML, not JSX, so remember to use class instead of className.

Comments on Current Trends and Updates (as of 2024):

  • ES Modules: The code in this tutorial was written in 2016, when CommonJS modules (require()) were standard. If you are working with a newer version of Node.js (version 14+), consider using ES modules (import and export) instead.
  • Template Engines vs. Client-side Rendering: Today, front-end frameworks like React, Vue, and Angular dominate for rendering. EJS still has its use cases, particularly for projects focused on server-side rendering and SEO, but consider the context of your application when choosing it.

Summary

In this tutorial, we covered the basics of using EJS for templating with Node.js. We learned how to install EJS, configure Express to use it, create layouts, and render content dynamically from the server. In the next video, we’ll move further into rendering React components on the server side and how to configure server-side rendering effectively.

Video Transcript

00:00It's time for us to get familiar with templating systems
00:04and more explicitly E-J-S-E-J-S is basically a way for you
00:09to create templates of HTML inside of Node
00:12and render them using the server.
00:14And that's called a template system.
00:16There's quite a few different ones.
00:17EJS is one of the easiest ones to work with
00:20and that's why we chose this one.
00:22So by the end of this Mess Less lesson video,
00:26we're gonna also learn how to use the render method,
00:28which actually renders out those
00:31templates whenever we need to.
00:32So let's see it in Action Alrightyy, let's continue.
00:37The next thing I want do is I want to install EJS,
00:40which is basically a templating mechanism that,
00:44let me just stop the server.
00:45A template lighting mechanism that enables us to basically
00:49dynamically output our view,
00:53which is incredibly powerful, incredibly useful.
00:55So let's go ahead and let's install it. Install.
00:58I wanna save it into our production EJS.
01:02Let's go ahead and save it. It's very, very simple to whoop.
01:05What did I do? NPM
01:09install in?
01:11Oh, I'm missing an N install. There we go.
01:14All right, let's jump right into our code editor now
01:16that we've got it installed.
01:18And I'm gonna go into my server again.
01:21And in my server, what I wanna do is I wanna make sure
01:24that we have EJS configured to be our review generator.
01:29To do that, I'm gonna go into my app right
01:31after I configure our public areas
01:34'cause it doesn't really concern them.
01:36And I'm gonna go to my app and I'm gonna go ahead
01:38and set and what am I gonna set?
01:40I'm gonna set my view engine and I'm gonna go ahead
01:43and set it to be EJS.
01:45And that's it. That's all I had to do to configure
01:48and basically tell the server to tell express
01:53that is gonna use EJS to render the views.
01:55Now there's a few things now that we're rendering views.
01:59One of the things you probably want to be aware of is
02:02that now we need to have a new folder
02:04that will contain all those views,
02:06which is different than our public area.
02:08Now the folder needs to be called views
02:10because that is the default folder that it expects.
02:13Now anything inside here could be anything else.
02:16I'm gonna go ahead and create a new folder,
02:18which I'm gonna call layout
02:19because really all I'm gonna have inside
02:21of this layout folder is really, really basic layouts.
02:25Now what I wanna do is I want to basically,
02:27and I'm just gonna go ahead here into one
02:30hour earlier files.
02:31I'm just gonna grab, register
02:33and copy it right inside into our layout.
02:36Once it's copied inside, I'm just gonna go ahead
02:38and make sure that it's actually gonna be called,
02:40let's call it just, uh, empty.
02:43And all file extensions
02:45for EJS files are conventionally called EJS as well.
02:50So I'm gonna use EJS and let's go into that file
02:53and lemme make sure that I put here too many letters. EJS,
02:59There we go.
03:00Okay, so now that we have that configured,
03:02let's go into our view, into our layout empty.
03:05And I'm just gonna go ahead
03:06and literally just empty everything out here.
03:08So I'm just gonna have an empty body with nothing
03:10inside for the moment.
03:12Now what I want to do is I'm gonna go ahead
03:15and show you how do you pass variables from your server
03:19into your layout
03:21and how do you say, what layout do you want to use?
03:23Okay, so let's go ahead
03:25and first of all, let's, let's first send
03:26the variable and then we'll use it.
03:28So instead of doing a, a send from our response,
03:32I'm gonna go to our response as well.
03:34But instead of that, I'm gonna call the render function
03:36and I'm gonna tell it what do I wanna render?
03:38I wanna render layout empty, which is that new file
03:41that we've created, no need for ejs.
03:44And then I'm gonna send an object with the variables
03:46that I wanna send into it.
03:48In this case, I wanna send a body in
03:49and I'm gonna call it, I get it.
03:53You got it? Nice. Alright, so I get it.
03:56Now, last but not least, I'm gonna go ahead and save this
03:59and go into EJS and where my body is, I'm gonna go ahead
04:02and open, uh, true, I don't know how you call that.
04:07Well, whatever the, I, I knew it a second ago.
04:09But anyways, open that bracket, the non round bracket
04:13and I'm gonna then put their, uh, this, this, that,
04:16and I'm gonna type body
04:17and let's go ahead and also close it.
04:20And there we go. And we've just outputted whatever
04:22the body is in there.
04:24And literally that's all we had to do to configure
04:26and get a really, really basic layout working with EJS.
04:31All that's left for us to do is to test it out
04:33to see if it's working, but just for the sake
04:36of making things a little bit nicer
04:38because really empty will just be an empty file.
04:40And in reality, we we're gonna want our register
04:43to use maybe a overlay, um, type of layout.
04:46So I'm gonna go ahead here and I'm just gonna copy it,
04:48duplicate it, and I'm gonna call this, uh,
04:52let's call it we said overlay.
04:56So anything that is an overlay will be called
04:58for that EJS file.
04:59Okay, so now what I wanna do next is I want
05:02to actually send into that overlay.
05:05Basically we're starting to go into eating into that world
05:09where we were with react js.
05:10So if you go back in your React js uh, chapter,
05:14if you recall in our build, we had our, not in our build
05:18inside of our app itself.
05:19And let me just open up for a second. We had a layout.
05:22What I'm gonna do is I'm just gonna grab that layout
05:25'cause in a, in a moment when we're gonna import it again
05:27and I'm gonna close, I just copy that layout
05:29and I'm gonna place that layout in that overlay right
05:32inside of our body.
05:34And I'm just gonna go ahead
05:35and make sure that our body is going to be sitting where
05:39that children used to be.
05:41And I'm also gonna make sure that we're not sending any, uh,
05:44Jake, we're not sending it not, sorry, not Jake.
05:46We're not, we're not sending any React stuff in here.
05:48So I'm gonna call his title. So these are the variables
05:51that EJS expects.
05:52So lemme go ahead and save this
05:54and lemme go to our server and instead of calling
05:56Empty, I'm gonna call overlay.
05:58But this time I'm also gonna give it a title
06:00because it's expecting two variables.
06:04And I'm gonna call our title register
06:09and let's go ahead and save and let's run our server again.
06:11I'm gonna go ahead and clear this and let's run our server.
06:16Where are you? Server? There we go. Let's run our server.
06:19And our server is running.
06:20And if I go back here and click your un refresh,
06:23we should now, uh, get our register and get it.
06:26We still need to clean up our design.
06:28And one of the problems with our design right now is if you
06:30look into your overlay
06:31and into empty, really any place where we were using, um,
06:36class name is not relevant anymore.
06:38Now we need to use class
06:39because we're not working on the client side way.
06:43Now we're working on the server and this is HTML.
06:45It's not JavaScript anymore, so we don't have
06:47to worry about using the wrong terms.
06:51And I could go back, get out clear, go back into my server,
06:55click you're on refresh and we should get now oto,
06:57our classes register.
07:00I got it. So there you go.
07:01Those are the basics of how you work
07:02with E-J-X-E-J-S-A templating
07:06system for no js.
07:09In this lecture, we saw how to use EJS, the template system,
07:14and in the next lecture we'll continue down our path,
07:16taking our React application that we've created,
07:19but moving it to be a server side component
07:22instead of a client side.
07:23One.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!