3logy
3logy

Reputation: 2712

How do i if/then in "mustache"-like underscore.js?

I use underscore.js for HTML Templating, set to use mustache syntax, like this: {{ }}

I have this code:

 <% if (typeof(date) != "undefined") { %>
  <span class="date"><%= date %></span>
 <% } %>

How can I translate it to an underscore.js mustache-style template, using {{ }}?

Upvotes: 10

Views: 3549

Answers (4)

Expert
Expert

Reputation: 1

just include this code after append underscore

_.templateSettings = {
    interpolate:/\{\{(.+?)\}\}/g
};

Upvotes: -1

charlysisto
charlysisto

Reputation: 3700

http://handlebarsjs.com/ is mustache with logic, partials, helpers & context. It can also be precompiled. A must IMHO.

Upvotes: 1

ggozad
ggozad

Reputation: 13105

I use:

    _.templateSettings = {
      evaluate : /\{\[([\s\S]+?)\]\}/g,
      interpolate : /\{\{([\s\S]+?)\}\}/g
    };

Then instead of <%= … %> use {{ … }} and instead of <% … %> use {[ … ]}

Upvotes: 19

spotman
spotman

Reputation: 857

{{#date}}
<span class="date">{{date}}</span>
{{/date}}

Upvotes: 0

Related Questions