Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

And why would I want to? Comes down to better separation of concerns - being able to separate HTML markup from Javascript.

What is a template?

  • A template is a section of HTML code that we want to inject into the browser

  • We often would like to populate the template with variable values (think mustache)

So we need a couple of things:

  • A way to load templates conveniently.

    • Javascript runs in a single threaded fast event loop in a browser.

    • Loading happens asynchronously, but it’s inconvenient to have to wait for templates to load before we can use them in Javascript code.

  • A way to parse templates and replace some of the HTML code with data from Javascript.

These are two separate concerns and therefore should be dealt with separatelyMustache is a fine templating library and liked by a lot of people. However I see these drawbacks:

  • 600 lines of code, 9 contributors for 20 line code problem - it’s therefore difficult to understand completely.

  • All data is automatically escaped for special characters in HTML.

  • It has no unnecessary features like

    • Template caching - which exchanges speed for memory usage and adds complexity. This is premature optimization. The simpler library will be faster by design.

    • There are no fancy callback features to tempt developers to write complicated code. It’s only going to work with an array of data.

    • No if statements if data isn’t present - encouraging separation of concerns.