Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

from underscore template to mustache.js

I would like to change from underscore template to mustache.js.

Since in mustache.js there are no if statements how can I change this piece of code in order to use mustache.js ?

<% if (done) { %>
<span class="todo-clear">
    <a href="#">
    Clear <span class="number-done"><%= done %></span>
    completed <span class="word-done"><%= done === 1 ? 'item' : 'items' %></span>
    </a>
</span>
<% } %>

My solution is:

 {{#total}}
        <span class="todo-count">{{ total }}
          <span class="number">{{ remaining }}</span>
          <span class="word"><%= remaining == 1 ? 'item' : 'items' %></span> left.-->
        </span>
          <span class="hint">
          Drag tasks from one list into another and vice versa.
          </span>
 {{/total}}

It works for total variable, because it could be 0 or more, but I have no idea what is the best way to fix it on remaining variable, which could be 1 or more.

<span class="word"><%= remaining == 1 ? 'item' : 'items' %></span> left.</span>

It should be something like that:

<span class="word">
    {{#remaining}} 'items'  {{/remaining}} 
    {{^remaining}} 'item'  {{/remaining}}
</span> 

It does not work because remaining could be 1 or more.

like image 207
underscore666 Avatar asked Apr 13 '12 08:04

underscore666


2 Answers

In your view you could make something like this:

Mustache.render(yourTemplate, {
     remaining: items.length > 1 ? true : false
}
like image 88
antonjs Avatar answered Nov 12 '22 10:11

antonjs


If your just moving to a new templating framework, I would recommend using handlebars.js. This has support for if statements as follows:

{{#if total}}
  <span>something</span>
{{else}}
  <span>something else</span>
{{/if}}
like image 25
Gwyn Howell Avatar answered Nov 12 '22 09:11

Gwyn Howell