from Hacker News

What Mustache can learn from Clojure

by michaelty on 11/27/11, 12:15 AM with 20 comments

  • by tomdale on 11/27/11, 1:40 AM

    Handlebars (handlebarsjs.com) solves several of these issues:

    Items in lists don't have to be objects with fields; you can display primitives like strings and numbers by using {{this}}:

      {{#each tags}}
        <li><a href="tags.html#{{this}}">{{this}}</a></li>
      {{/each}}
    
    Shadowing properties from parent context objects isn't a problem. Handlebars allows you to reach parent contexts with a '../' notation:

      {{#each comments}}
        <h2><a href="/posts/{{../permalink}}#{{id}}">{{title}}</a></h2>
        <div>{{body}}</div>
      {{/each}}
    
    Regarding looping and conditional branching, Handlebars has an {{#if}} helper that does not evaluate to true for empty arrays:

      {{#if authors}}
        {{#each authors}}
          {{firstName}} {{lastName}}
        {{/each}}
      {{else}}
        No authors to display.
      {{/if}}
    
    I don't fully understand the complaint about recursive data structures. I usually just register a helper that can invoke itself recursively.
  • by bascule on 11/27/11, 1:32 AM

    As a user of Clojure and an onlooker of things like mustache (and handlebars) who has used neither of the latter, this post left me incredibly confused. I like the points about destructuring and having something like a let binding. Those seem like useful addition.

    However, about half the post discusses how "looping and conditional branching are conflated", and the end focuses on adding a separate branching construct to what's supposed to be a logic-free templating language. So I guess what he's trying to say is he wants to add logic to a logic-free templating language. It seems like he missed the point of Mustache: Looping and branching are conflated by design to eliminate logic from the template.

    About the only way I see the connection between Clojure and Mustache at all is through the potential value destructuring could bring, and destructuring isn't exactly a feature which is unique to Clojure (CoffeeScript, Scala, and Erlang come to mind). Otherwise this post is apples and oranges: Clojure macros are about as far as you can get from a logic-free templating language. I'm wondering if the additions he proposes are sufficient to turn what's supposed to be a logic-free templating language into a Turing-complete one (see this example of why XSLT is Turing-complete: http://www.unidex.com/turing/utm.htm)

  • by stevelosh on 11/27/11, 1:25 AM

        However, there appears to be a set of common denominators
        that can be used to represent all possible 
        data-structures: Maps or dictionaries, lists, strings, 
        numbers, boolean and null.
    
    I'd add sets to this list. You can kind of simulate them using a map where the keys are the members and the values are anything, but they just work more smoothly when you have real support for them (like in Python and Clojure, but not Javascript/JSON).
  • by LeafStorm on 11/27/11, 3:33 AM

    To be honest, I have never seen the appeal of logic-less templates to begin with. I can understand wanting a sandboxable template engine independent of language, but that doesn't necessarily translate to "lack of logic."

    And yeah, it's easier to shoot yourself in the foot with templates that have logic, but on the other hand with logic-less templates you may need to do something that the template language won't let you do, and then you have to go and write some more code in your domain logic. Which means that you're doing something even worse than pushing your business logic into your display logic - you're pushing your display logic into your business logic!

  • by grayrest on 11/27/11, 3:04 AM

    > Items in lists must be objects with fields because it is always decomposed, and there is no way to get a handle on the list item itself.

        {{%IMPLICIT-ITERATOR}}
        {{#foo}}
          {{.}}
        {{/foo}}
    
    But...Handlebars.
  • by leeoniya on 11/27/11, 7:06 AM

    i've also had a few quibbles about the way Mustache is specced - i felt like it was 80% there, but forced too-verbose templates. i re-implemented and modified it a bit to alleviate some of the ambiguity. never had a chance to do full docs, but the js code/readme is up on github if anyone wants to check it out: https://github.com/leeoniya/handlebar.js