Tag Archives: Templates

Mustache

I saw an interesting approach to view rendering.  Often in web projects, the template or views will grow to include many conditionals.  You will start to see conditions littered about. Some get to contain logic like this:

<% If (someObject.Count() > 0) { %>
  <div>Something...</div>
<% } %>
Enter Mustache. In an effort to clean up the views, it doesn’t allow conditions.  It is listed as a logic-less templating system.  It can deal with lists of items or showing a section based on a boolean.  That’s it.  It doesn’t calculate whether a section can be shown.  All calculations must be done before the model is added to the template.
An example might be:
{{#premium_customer}}
  You saved ${{dollar_savings}} by joining our premium service!
{{/premium_customer}}
The data bound to that would be something like this (JSON):

{
     "premium_customer": true,
     "dollar_savings": 14.1
}
It forces you to prepare all the logic before it gets to the view.  I like the concept and want to try it with dozens of views to see if it holds up.  For more details about mustache usage, see the manual.