On Fri, Dec 27, 2013 at 4:54 PM, Ryan Kaldari <rkaldari(a)wikimedia.org>wrote;wrote:
On Fri, Dec 27, 2013 at 4:36 PM, Tyler Romeo
<tylerromeo(a)gmail.com> wrote:
As in any type of control structure. Mustache is
explicitly a template
language without control structures. You can technically implement
if/else
statements and for loops using Mustache's
sections, but you can't have
expressions in your if statements. Template languages like Twig are more
like an actual programming language.
Having a template library that is essentially a programming language is
something I would prefer to avoid. The entire point of using a template
library (IMO) is to separate the markup from the logic. We can still use
PHP or JS to process anything that needs serious munging.
I believe Twig does this better than Mustache. Twig does allow for complex
for loops and if statements, but it's all about manipulating the rendered
output. Try to think how you would implement a pager in Twig vs Mustache.
In twig you can pass in a 'page' parameter and using callbacks manipulate
the conditions of the foreach loop. In mustache an outside function would
have to manipulate the data set before passing it in. Basically; I'm in the
camp that the templating system should have access to all the data just in
case it decides to use it rather than causing everyone to build frameworks
around it.
Mustache also has another weakness that the above exposes -- In twig you
must explicitly register each callback / filter allowing one to build a
library. In Mustache you must pass anonymous functions to be passed in the
data hash. So now the data to be rendered has code in it that's language
and probably template dependent necessitating a preprocessing layer to
inject those functions, or otherwise manipulate the data, depending on what
language you're in.
Another couple of use cases we can analyse, that I feel are things that are
properly in the display layer rather than having to manipulate them without
being aware of display context in the backend (basically I'm making a case
for decoupling):
For the fundraising thank you letters, I pass a dictionary to the template
containing the currency string "VND 20000.23"; via callback that gets
transformed into 20,000.23*₫* via a i18n library that I wrote.
I pass in details about a contribution (such as ID, date, email, name) to
the template, and then these get composited into things like links,
personalized greetings, and displayed tables.
~Matt