5 ms·
When I see examples like this, it reminds me just how elegant & simple Mustache.js template can be: Mustache.render( "{{#loggedIn}}Hello{{userName}}{/loggedI
by orange888 10y ago
When I see examples like this, it reminds me just how elegant & simple Mustache.js template can be:
Mustache.render( "{{#loggedIn}}Hello{{userName}}{/loggedIn}}
{{^loggedIn}}Please log in{{/loggedIn}}", state );
- yeskia 10y agoElegant & simple?
- orange8888 10y agoElegant & simple!
- bschwindHN 10y agoWhat exactly is elegant and simple about that? I feel like this kind of syntax would break down on more complex examples, is that not the case? What would it look like with multiple conditions (say something like A && B || C)?
- orange8888 10y agoWhat on earth does not break given more complex examples? That's a bit of a non-argument. The mustache example is a direct translation of the jsx example, so keep things in context. As for your multiple conditions question, what exactly is the point of that? All l see is code, no data, no markup. Completely irrelevant.
- bschwindHN 10y agoNo need to get so defensive. I'm just curious how it holds up in more complex examples. I know how JSX does because you just use the same concepts you already know from JS. But Mustache seems to have its own custom syntax, so how well can it express more complex cases? After all, we're not just displaying "Hello" or "Please log in" all day.
- orange888 10y agoI simply responded in kind. Notice the difference in tone from your first response, and this one. To answer you question, you use JS! Or PHP! Or any of the other 2 dozen languages Mustache supports. Mustache is all about logic-less templates, which by definition assumes the logic is being written elsewhere. Edited my example to make that clearer. EDIT: const tpl = "{{#loggedIn}}Hello{{userName}}{/loggedIn}}{{^loggedIn}}Please log in{{/loggedIn}}"; Mustache.render( tpl, state );
- bschwindHN 10y agoHmm, I see what you mean about pushing logic to the code calling Mustache, but I still wouldn't call it elegant with all the curly braces and explicit closing tags needed. JSX isn't elegant either in that regard which is why I work with React through ClojureScript.
- orange888 10y agoExplicit closing tags are only needed when dealing with more complex examples, like basic conditional or loops (arrays). Rather than try to teach you on a hackernews forum, you can learn all you need to know about the syntax in 5 minutes here: https://mustache.github.io/mustache.5.html https://mustache.github.io/mustache.5.html There is even a ClojureScript implementation: https://github.com/fotoetienne/cljstache https://github.com/fotoetienne/cljstache Yes mustache, JSX and all the other templating syntax aren't pretty, but my point was that Mustache is the best I've come across so far. elegance and simplicity are in the eyes of the beholder though, so to each their own.