Book Image

Learning Single-page Web Application Development

Book Image

Learning Single-page Web Application Development

Overview of this book

Table of Contents (15 chapters)
Learning Single-page Web Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Templates with Embedded JavaScript


At the beginning of the chapter, we adopted the mechanism of templates called EJS, which is very easy to understand. Besides, EJS gives us the template logic functionality within its syntax and can be easily embedded within HTML files. Its use is very similar to that of Handlebars, another excellent JavaScript library to use templates; for example:

<h1><%= title %></h1>
<ul>
  <% for( var i = 0; i < bands.length; i++ ) { %>
    <li>
      <%= bands[i] %>
    </li>
  <% } %>
</ul>

It is very similar to other programming languages such as PHP and RoR.

However, you can use the template that suits you; Node.js has a great list of template engines for all tastes.

Tip

Another good option for a template engine is Swig, which can be installed through the npm command:

npm install swig
  1. Let's create the following files' templates in the views folder:

    • login.ejs

    • profile.ejs

    • signup.ejs

  2. Also, we will create a...