← Back to team overview

launchpad-dev team mailing list archive

Re: 'stache, handlebars, tal and so forth

 

On Mon, 30 Jan 2012, Marc Tardif wrote:

> * Richard Harding <rick.harding@xxxxxxxxxxxxx> [2012-01-30 08:26 -0500]:
> > On Fri, 27 Jan 2012, Marc Tardif wrote:
> [snip]
> > > 1. Make two passes over the data, once to massage the data and another
> > >    time to render it. In other words, first render the raw JSON to cooked
> > >    JSON and then the cooked JSON to HTML. I'm not so much concerned about
> > >    the performance implication of rendering the same data twice as much
> > >    as the nuisance of duplicating what is essentially display logic in
> > >    two places.
> >
> > There are a couple of ways this might go. Mustache does support
> > "functions" or lambda depending on which docs you read. So you could add a
> > function that does some date parsing logic to your objects as you pass them
> > to the template and that function would accept the date string coming from
> > the JSON and then try to pretty it up.
>
> My understanding of the mustache.js documentation is that functions are
> indeed supported but they are assumed to be contained in the JSON data
> passed to the renderer. In other words, lets say I received something
> like this from the API:
>
> ...
>
> To render the last_test_run_date properly, I would have to cook the view
> to contain somekind of rendering function like this:
>
> ...
>
> If my understanding is correct, I'm rather uncomfortable with the idea of
> having to cook the raw data like this. Otherwise, please let me know how
> else this problem might be solved with mustache.js.

Yes, you are correct. This then gets into why some of us aren't the biggest
fans of Mustache as a template language. A very similar language with
similar syntax is Handlebars. The nice thing about handlebars is that it
supports the idea of helpers.

http://handlebarsjs.com/#helpers

If you follow that idea you could see writing a json_dateparse helper that
you could register and use in your template

...

{{each model}}
<li>Stored Date: {{json_datepare stored_date}}</li>
{{/each}}


--

Rick Harding

Launchpad Developer
https://launchpad.net/~rharding
@mitechie


Follow ups

References