Rendering objects using EJS
EJS is a template language that allows users to mix HTML and JavaScript inside the template. Similar to PHP and ERB, it works by adding extra tags to HTML which allow the user to "escape" from HTML to the programming language and use the full facilities of that language.
In this recipe, we're going to demonstrate EJS using a simple example. We're going to render a user greeting based on the time of the day.
Getting ready
We need to download EJS from http://embeddedjs.com/ and extract ejs_production.js
in our recipe
folder.
How to do it...
Let's get started.
Create
index.html
containing aname
input, agreeting
placeholder, and an EJStemplate
:<!DOCTYPE HTML> <html> <head> <title>Displaying an EJS object</title> </head> <body> <form method="post"> <p>Name: <input id="name" type="text" name="name" value="John"></p> </form> <div id="greeting"> </div> <script id="template" type="text...