views:

40

answers:

2

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?

A: 

It makes your templates cleaner, and it forces you to keep logic in a place where it can be properly unit-tested.

blockhead
Can you explain it with more details?
Morgan Cheng
Spend three months working on a system using a logicful templating language, like JSP, with programmers who are not zealous about separating logic and presentation. You'll find you build a system which essentially has programming in the page - arithmetic for table layout, conditionals for what pricing information to show, and so on. Templating languages are extremely poor programming languages, so this makes for a development and maintenance nightmare. Something like Mustache doesn't let you get into that situation in the first place.
Tom Anderson
A: 

A logic-less template is a template that contains holes for you to fill, and not how you fill them. The logic is placed elsewhere and mapped directly to the template. This separation of concerns is ideal because then the template can easily be built with different logic, or even with a different programming language.

From the mustache manual:

We call it "logic-less" because there are no if statements, else clauses, or for loops. Instead there are only tags. Some tags are replaced with a value, some nothing, and others a series of values. This document explains the different types of Mustache tags.

Jeremy
The description is slightly sophistic, as the sections work rather like conditionals and loops, just very limited ones. The ability to refer to callables in the hash also lets you transform sections into a rudimentary programming language. Still, at least it makes it difficuly to go down that route, rather than encouraging it.
Tom Anderson
Sure, but a template system without blocks for conditions and iteration would be relatively useless. The template itself doesn't specify what the block is for, or how it's handled.
Jeremy