How to create templates with Javascript?

Question:

I would like to know if it is possible (and how to do it, if it is) templates with Javascript . Just like Facelets in JSF , where you create a template page and the others follow what was pre-established.

If anyone can indicate any material, I really appreciate it!

Answer:

With Handlebars you'll be able to create js templates with ease!

See an example of its syntax/Usage:

<div class="entry">
  <h1>{{title}}</h1>
  <div class="body">
    {{body}}
  </div>
</div>

Using {{template}} to make your markings.

Populating the template.

var context = {title: "My New Post", body: "This is my first post!"};
var html    = template(context);

Result:

<div class="entry">
  <h1>My New Post</h1>
  <div class="body">
    This is my first post!
  </div>
</div>

Another framework that can do this is AngularJS, it is very complete it has:

  • Two-way Data Binding
  • Dependency Injection
  • Creating directives (HTML Extension) <- Template
  • Modularization and reuse (Controllers, Services and Filters)
Scroll to Top