Chapter 3. Working with templates

 

This chapter covers

  • Creating templates
  • Using Meteor’s default templating syntax
  • Organizing JavaScript and HTML
  • Using event maps to make templates interactive
  • Understanding the template life cycle

Everything your web browser renders is HTML eventually. If you open any website in source view you’ll find that each page has hundreds of lines of HTML. Not only is writing everything by hand tedious, error-prone, and highly inefficient, it’s also impossible when you’re working with web applications because much of the content is dynamic and you can’t know in advance what exact code needs to be rendered.

Templates allow you to define blocks of HTML that can be reused as often as you like. As a result, you have to write less code, and your code is easier to read and much simpler to maintain.

In this chapter, you’ll learn how to write templates and what you can do with them. We’ll first take a look at Blaze, Meteor’s reactive UI library, and Spacebars, its default templating language.

3.1. Introduction to templates

Templates are the building blocks for our UI. Templates are snippets of HTML that contain placeholders, which can be filled by content returned by the application logic. They can be reused to ensure the same look and feel on all sites and all elements.

3.2. Working with templates

3.3. Creating dynamic HTML templates

3.4. Handling events

3.5. The template life cycle

3.6. Summary