Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the advantage of Logic-less template (such as mustache)?

Recently, I ran into mustache which is claimed to be Logic-less template.

However, there is no explaining why it is designed in Logic-less way. In another word, what's the advantage of Logic-less template?

like image 611
Morgan Cheng Avatar asked Oct 09 '10 09:10

Morgan Cheng


People also ask

What is in mustache template?

The Mustache templates consist of tag names surrounded by { { } } (which resemble mustaches – hence the name) and are backed by a model object containing the data for the template.

What is logic-less?

logicless (comparative more logicless, superlative most logicless) Without logic; alogical or illogical.

Are handlebars Logicless?

"Handlebars is a logic-less templating engine, which means there is little to no logic in your templates that are on the HTML page.


2 Answers

In other words, it prevents you from shooting yourself in the foot. In the old JSP days, it was very common to have JSP files sprinkled with Java code, which made refactoring much harder, since you had your code scattered.

If you prevent logic in templates by design (like mustache does), you will be obliged to put the logic elsewhere, so your templates will end up uncluttered.

Another advantage is that you are forced to think in terms of separation of concerns: your controller or logic code will have to do the data massaging before sending data to the UI. If you later switch your template for another (let's say you start using a different templating engine), the transition would be easy because you only had to implement UI details (since there's no logic on the template, remember).

like image 109
Miguel Ping Avatar answered Oct 19 '22 11:10

Miguel Ping


I get the feeling that I am nearly alone in my opinion, but I am firmly in the opposite camp. I don't believe that the possible mixing of business logic in your templates is enough reason not to use the full power of your programming language.

The usual argument for logic-less templates is that if you have full access to your programming language you might mix in logic that has no place being in a template. I find this akin to reasoning that you should use a spoon to slice meat because you could cut yourself if you use a knife. This is very true, and yet you will be far more productive if you use the latter, albeit carefully.

For instance, consider the following template snippet using mustache:

{{name}}: <ul>   {{#items}}     <li>{{.}}</li>   {{/items}} </ul> 

I can understand this, but I find the following (using underscore) to be much more simple and direct:

<%- name %>: <ul> <% _.each(items, function(i){ %>   <li><%- i %></li> <% }); %> </ul> 

That being said, I do understand that logicless templates have advantages (for instance, they can be used with multiple programming languages without changes). I think these other advantages are very important. I just don't think their logic-less nature is one of them.

like image 44
brad Avatar answered Oct 19 '22 11:10

brad