underscore666
underscore666

Reputation: 1739

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.

Upvotes: 5

Views: 1072

Answers (2)

Gwyn Howell
Gwyn Howell

Reputation: 5424

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}}

Upvotes: 2

antonjs
antonjs

Reputation: 14318

In your view you could make something like this:

Mustache.render(yourTemplate, {
     remaining: items.length > 1 ? true : false
}

Upvotes: 3

Related Questions