Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>With Mustache it's possible to differentiate between the value <code>0</code> and non-<code>0</code> values using the <code>{{#section}}</code> and <code>{{^inverted}}</code> tags:</p> <pre><code>&lt;!-- If number_of_hits is not 0, show hits --&gt; {{#number_of_hits}} {{number_of_hits}} hits {{/number_of_hits}} &lt;!-- If number of hits is 0 (or any other falsy value) --&gt; {{^number_of_hits}} No hits {{/number_of_hits}} </code></pre> <p>As far as I know, Mustache cannot detect the difference between 1 and 2, for instance. For that you'll have to modify the <code>view</code>object you pass into the template. Possibly something like:</p> <pre><code>var hits = 3; var view = { search_term: 'something', number_of_hits: hits, hitkind: { none: hits === 0, one: hits === 1, many: hits &gt; 1 } }; </code></pre> <p>And in the template</p> <pre><code>{{#hitkind.none}} No hits {{/hitkind.none}} {{#hitkind.one }} One hit {{/hitkind.one}} {{#hitkind.many}} {{number_of_hits}} hits {{/hitkind.many}} </code></pre> <p>Alternatively you could consider changing your templating engine to <a href="http://handlebarsjs.com/" rel="nofollow">Handlebars.js</a>. Handlebars is a superset of Mustache, which means that all your Mustache templates will work with it. Handlebars templates are logicless just like Mustache's, so it doesn't enable you to write the logic directly to the template. Instead it provides a concept of <code>Helpers</code>, or functions callable from your templates.</p> <p>With Handlebars you can define a helper, like:</p> <pre><code>Handlebars.registerHelper('hitCount', function(count) { if(count === 0) return "No hits"; if(count === 1) return "One hit"; return count + " hits". }); </code></pre> <p>And call it from a template:</p> <pre><code>{{hitCount number_of_hits}} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload