Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Taking the solution one step further. This adds the compare operator.</p> <pre><code>Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) { switch (operator) { case '==': return (v1 == v2) ? options.fn(this) : options.inverse(this); case '===': return (v1 === v2) ? options.fn(this) : options.inverse(this); case '!=': return (v1 != v2) ? options.fn(this) : options.inverse(this); case '!==': return (v1 !== v2) ? options.fn(this) : options.inverse(this); case '&lt;': return (v1 &lt; v2) ? options.fn(this) : options.inverse(this); case '&lt;=': return (v1 &lt;= v2) ? options.fn(this) : options.inverse(this); case '&gt;': return (v1 &gt; v2) ? options.fn(this) : options.inverse(this); case '&gt;=': return (v1 &gt;= v2) ? options.fn(this) : options.inverse(this); case '&amp;&amp;': return (v1 &amp;&amp; v2) ? options.fn(this) : options.inverse(this); case '||': return (v1 || v2) ? options.fn(this) : options.inverse(this); default: return options.inverse(this); } }); </code></pre> <p>Use it in a template like this:</p> <pre><code>{{#ifCond var1 '==' var2}} </code></pre> <p>Coffee Script version</p> <pre><code>Handlebars.registerHelper 'ifCond', (v1, operator, v2, options) -&gt; switch operator when '==', '===', 'is' return if v1 is v2 then options.fn this else options.inverse this when '!=', '!==' return if v1 != v2 then options.fn this else options.inverse this when '&lt;' return if v1 &lt; v2 then options.fn this else options.inverse this when '&lt;=' return if v1 &lt;= v2 then options.fn this else options.inverse this when '&gt;' return if v1 &gt; v2 then options.fn this else options.inverse this when '&gt;=' return if v1 &gt;= v2 then options.fn this else options.inverse this when '&amp;&amp;', 'and' return if v1 and v2 then options.fn this else options.inverse this when '||', 'or' return if v1 or v2 then options.fn this else options.inverse this else return options.inverse this </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