TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

What Mustache can learn from Clojure

23 点作者 michaelty超过 13 年前

6 条评论

tomdale超过 13 年前
Handlebars (handlebarsjs.com) solves several of these issues:<p>Items in lists don't have to be objects with fields; you can display primitives like strings and numbers by using {{this}}:<p><pre><code> {{#each tags}} &#60;li&#62;&#60;a href="tags.html#{{this}}"&#62;{{this}}&#60;/a&#62;&#60;/li&#62; {{/each}} </code></pre> Shadowing properties from parent context objects isn't a problem. Handlebars allows you to reach parent contexts with a '../' notation:<p><pre><code> {{#each comments}} &#60;h2&#62;&#60;a href="/posts/{{../permalink}}#{{id}}"&#62;{{title}}&#60;/a&#62;&#60;/h2&#62; &#60;div&#62;{{body}}&#60;/div&#62; {{/each}} </code></pre> Regarding looping and conditional branching, Handlebars has an {{#if}} helper that does not evaluate to true for empty arrays:<p><pre><code> {{#if authors}} {{#each authors}} {{firstName}} {{lastName}} {{/each}} {{else}} No authors to display. {{/if}} </code></pre> I don't fully understand the complaint about recursive data structures. I usually just register a helper that can invoke itself recursively.
评论 #3281373 未加载
bascule超过 13 年前
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.<p>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 <i>by design</i> to eliminate logic from the template.<p>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: <a href="http://www.unidex.com/turing/utm.htm" rel="nofollow">http://www.unidex.com/turing/utm.htm</a>)
评论 #3281324 未加载
评论 #3292576 未加载
stevelosh超过 13 年前
<p><pre><code> 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. </code></pre> 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).
评论 #3292425 未加载
LeafStorm超过 13 年前
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."<p>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!
评论 #3281703 未加载
评论 #3281702 未加载
grayrest超过 13 年前
&#62; 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.<p><pre><code> {{%IMPLICIT-ITERATOR}} {{#foo}} {{.}} {{/foo}} </code></pre> But...Handlebars.
评论 #3292398 未加载
评论 #3281558 未加载
leeoniya超过 13 年前
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: <a href="https://github.com/leeoniya/handlebar.js" rel="nofollow">https://github.com/leeoniya/handlebar.js</a>
评论 #3282935 未加载