Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 814
3logy Avatar asked Jan 24 '12 17:01

3logy


2 Answers

I use:

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

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

like image 141
ggozad Avatar answered Oct 20 '22 02:10

ggozad


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

like image 34
charlysisto Avatar answered Oct 20 '22 01:10

charlysisto