Printing out simple calculations in Mustache/Handlebars

2,618 views
Skip to first unread message

Marc Schipperheyn

unread,
Jul 23, 2012, 5:15:05 PM7/23/12
to mustac...@googlegroups.com
When I first learned about Mustache/Handlebars I thought it could be the holy grail of cross platform development. I work in Java and Javascript and I thought this could make it possible to write cross platform page fragments. With YUI and Handlebars.java, I was good to go.

Now that I have a little more experience with Mustache/Handlebars my initial enthusiasm has cooled a little. Simply, because with Handlebars, extremely trivial things can be hard to implement. 

Take the simple example of doing some on page arithmetics:
1+3

Should be dead simple, 2 milliseconds work, right? I'll tell you the truth: I still haven't figured it out. 
According to some of the examples I saw you're supposed to put a function in your data structure that handles this. No way, I'm mixing data and logic like that. 
So, I found myself writing more and more helper functions in Java and Javascript. Simple enough although Mustache/Handlebars stimulates way too much of these AFAIC. 
But now, how to call this function if part of your artithmatics is a variable?

{{eval myVar.id * 10}}

No idea how to deal with this.

Cheers,
Marc

Sam Pullara

unread,
Jul 23, 2012, 5:35:44 PM7/23/12
to mustac...@googlegroups.com
The best practice when using mustache is to introduce a new type of
object - view code - that binds tightly to the template, rather than
using data objects directly:

mustache template -> view code -> data

The view code is responsible for doing calculations and
transformations on the data so that is usable within the template. At
Twitter we use the same templates with Java/Scala, Ruby and Javascript
using this model. When moving from one language to another typically
the view code is the only layer that really needs to change as long as
they both have access to the data.

Sam

Marc Schipperheyn

unread,
Jul 23, 2012, 5:51:52 PM7/23/12
to mustac...@googlegroups.com
So, what you're saying is that it's probably smarter to invest time in getting the model perfectly aligned with the template (capabilities) than writing helpers that help you modify whatever data is provided.
I suppose it makes sense, but I still find it hard to swallow that trivial things are not trivial to handle. 

Cheers,

Marc

Sam Pullara

unread,
Jul 23, 2012, 6:08:58 PM7/23/12
to mustac...@googlegroups.com
Well, I would say that the view code and the model are generally
separate. Here is a random example:

template:
<span>{{explicit_timestamp}}</span>

backing view code:
class View {
Status status;
View(Status status) {
this.status = status;
}
String explicit_timestamp() {
return new SimpleDateFormat("hh:mm aa MMM dd").format(new
Date(status.createdAt()))
}
}

model:
class Status {
long createdAt();
}

There is no reason for the model to know how the view might want to
format it, just as there is no reason for the template to assume a
formatting either. So, when it comes time to internationalize this or
port it to another language, the only code that needs to change / be
written is in the backing view code.

Sam

On Mon, Jul 23, 2012 at 2:51 PM, Marc Schipperheyn
Reply all
Reply to author
Forward
0 new messages