2

Could someone please tell me how to change the default delimiter for mustache.js? The default delimiter is {{var}} and I would like to change it to {|var|}

I have the following code:

$('body').append(Mustache.render(this.template, data));

Many thanks

Peter J Harrison
  • 641
  • 2
  • 12
  • 21

4 Answers4

8

As per the documentation:

Set Delimiter tags start with an equals sign and change the tag delimiters from {{ and }} to custom strings. Consider the following contrived example:

* {{ default_tags }}
{{=<% %>=}}
* <% erb_style_tags %>
<%={{ }}=%>
* {{ default_tags_again }}

So for your case where you want to use {|var|} you could probably use:

{{={| |}=}}

Note, here is another example that changes the default delimiter to triple-mustaches.

Community
  • 1
  • 1
Casey Falk
  • 2,617
  • 1
  • 18
  • 29
  • thanks Casey, so do I need to add anything to the line of code: $('body').append(Mustache.render(this.template, data)); or do I just update the mustache tags in my template? Im confused around how the mustache lib knows I'm using custom delimiters? – Peter J Harrison Jul 14 '14 at 22:08
  • The documentation and example seem to imply that that is all you need. Notice the equals signs in the code above; you literally are setting each template tag start/end to a new value. It probably checks the direction of the assignment and the current assigned values to check which part of the template tag to change. Make sense? – Casey Falk Jul 14 '14 at 22:12
  • Why the downvote? Is this no longer the standard method? – Casey Falk Feb 15 '15 at 21:17
  • 2
    Just add the delimiter change before your template, something along the lines of `$('body').append(Mustache.render('{{={| |}=}} ' + this.template, data));` worked for me. – nicja Aug 28 '15 at 09:21
  • I need exactly this feature because I use mustache for TeX and there I also need "unescaped" fields, and the "&" operator comes in handy here: instead of `{{{field}}}` it is possible to write - with custom delimiters `<%` and `%>` the unescaped field as `<% & field %>`. – peschü Aug 11 '19 at 09:53
1

I think a javascript example will be helpful too.

var template = $('#template').html();
var parseTags = new Array();
parseTags.push("[[");
parseTags.push("]]");
Mustache.parse(template,parseTags);   // optional, speeds up future uses
var rendered = Mustache.render(template, {name: "<%Luke%>"});
rastasheep
  • 10,416
  • 3
  • 27
  • 37
Jeff Chung
  • 176
  • 3
  • 13
0

In the app.js, use the last line in the following snippet. The first 2 are for reference:

// view engine setup
app.set('views', path.join(__dirname, 'views/templates'));
app.set('view engine', 'hjs');
app.locals.delimiters = '{| |}';
Peter Graham
  • 2,467
  • 2
  • 24
  • 29
0

There's a much neater way to do this now and it's documented well. https://github.com/janl/mustache.js/

var customTags = [ '<%', '%>' ];

Mustache.render(template, view, {}, customTags);
//Either tell mustache to use them each time
Mustache.render(template, view, {}, customTags);
//Or override the tags property and Mustache knows what to do until you tell it otherwise
Mustache.tags = customTags;
var updateTemplate = '<small class="text-muted">Last updated <% updated_at %> by <% updated_by %></small>';
var html = Mustache.render(updateTemplate, {updated_at:'2019-11-18 14:54:20',updated_by:'abc'});
aberpaul
  • 437
  • 3
  • 6