Best Practices When Working With JavaScript Templates

A few best practices that you should keep in mind, when using templates

Maybe you don't need them for simple web apps, but it doesn't take too much complexity before embracing JavaScript templates becomes a good decision. Like any other tool or technique, there are a few best practices that you should keep in mind, when using templates. Andrew Burgess will take a look at a handful of these practices in this tutorial.

  • Underscore for Simple, Handlebars for Complex
  • Create Template Helper Functions
  • Store Templates in Their Own File
  • Precompile Your Templates
  • No Evaluation in Templates
  • Bind Data to the Templates
  • Simplify Your Templates
  • Don't Use 'em if You Don't Need 'em
Underscore for Simple, Handlebars for Complex
First things first, you most likely want to choose a well-tested library to offer your template functionality. If the project is relatively simple, you can use Underscore.js. For the most part, this library offers functional programming utilities, but it does have a _.template method that couldn't make things easier. By default, it uses the ERB-esque <%= %> delimiters, but that can easily be modified. The beauty of this solution is that any project that requires templates will likely have Underscore loaded already, simply due to its sheer general usefulness.

Comments

Be the first to write a comment

You must me logged in to write a comment.