Questions tagged [mustache]

Mustache is a "logic-less" templating language available in a range of languages.

Mustache provides "logic-less templates". It has implementations in , , , , , , , , , , , , , , , , , , and . It supports lists and lambdas, but it does not allow for embedded program logic (as in, for example, Django templates). As implied by the tagline "logic-less templates", that exclusion of logic is by design.

Mustache markup is very simple:

<nav>
    <h1>{{title}}</h1>
    <ul>{{#navList}}
        <li><a href="{{link}}">{{text}}</a></li>
    {{/navList}}</ul>
</nav>

The structure of the HTML and {{mustache}} markup play very nicely together.

Meanwhile, in JavaScript for example, the usage of the template couldn’t be simpler:

var navhtm = ... //Here would be some code to pull in template HTML*
var navobj = { //Data structure to define a menu
                 title : "Main Menu",
                 navList : [
                     { link: "page1.html", text: "Page 1" },
                     { link: "page2.html", text: "Page 2" },
                     { link: "pageC.html", text: "Page C" }
                 ]
             ;
//Of course, that data structure could come from the server or elsewhere...

//Render the template and stick it into the id="Nav" element using jQuery.
$("#Nav").html(Mustache.to_html(navhtm,navobj));

This will result in the id="Nav" tag being populated with this HTML:

<nav>
    <h1>Main Menu</h1>
    <ul>
        <li><a href="page1.html">Page 1</li>
        <li><a href="page2.html">Page 2</li>
        <li><a href="pageC.html">Page C</li>
    </ul>
</nav>
1949 questions
385
votes
9 answers

What are the differences between Mustache.js and Handlebars.js?

Major differences I've seen are: Handlebars adds #if, #unless, #with, and #each Handlebars adds helpers Handlebars templates are compiled (Mustache can be too) Handlebars supports paths Allows use of {{this}} in blocks (which outputs the current…
Chad Johnson
  • 21,215
  • 34
  • 109
  • 207
305
votes
5 answers

How do I accomplish an if/else in mustache.js?

It seems rather odd that I can't figure how to do this in mustache. Is it supported? This is my sad attempt at trying: {{#author}} {{#avatar}} {{/avatar}} {{#!avatar}}
egervari
  • 22,372
  • 32
  • 121
  • 175
234
votes
8 answers

Is there a built-in way to loop through the properties of an object?

Is there a Mustache / Handlebars way of looping through an object properties? So with var o = { bob : 'For sure', roger: 'Unknown', donkey: 'What an ass' } Can I then do something in the template engine that would be equivalent to for(var…
Ben
  • 20,737
  • 12
  • 71
  • 115
115
votes
6 answers

Can mustache iterate a top-level array?

My object looks like this: ['foo','bar','baz'] And I want to use a mustache template to produce from it something like this: "
  • foo
  • bar
  • baz
" But how? Do I really have to munge it into something like this…
greim
  • 9,149
  • 6
  • 34
  • 35
111
votes
13 answers

What's the advantage of Logic-less template (such as mustache)?

Recently, I ran into mustache which is claimed to be Logic-less template. However, there is no explaining why it is designed in Logic-less way. In another word, what's the advantage of Logic-less template?
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230
102
votes
19 answers

In Mustache templating is there an elegant way of expressing a comma separated list without the trailing comma?

I am using the Mustache templating library and trying to generate a comma separated list without a trailing comma, e.g. red, green, blue Creating a list with the trailing comma is straightforward, given the structure { "items": [ {"name":…
John Kane
  • 3,601
  • 5
  • 23
  • 18
93
votes
5 answers

How to handle an IF STATEMENT in a Mustache template?

I'm using mustache. I'm generating a list of notifications. A notification JSON object looks like: [{"id":1364,"read":true,"author_id":30,"author_name":"Mr A","author_photo":"image.jpg","story":"wants to…
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
71
votes
1 answer

Mustache template string inside render as HTML

I am using Mustache to render templates. I have this json object: { title: "Foo bar", content: "

Html here

", footer: "footer content here" } I have a Mustache template like:
Snow_Mac
  • 5,727
  • 17
  • 54
  • 80
62
votes
13 answers

In Mustache, How to get the index of the current Section

I am using Mustache and using the data { "names": [ {"name":"John"}, {"name":"Mary"} ] } My mustache template is: {{#names}} {{name}} {{/names}} What I want to be able to do is to get an index of the current number in the array. Something…
christophercotton
  • 5,829
  • 2
  • 34
  • 49
61
votes
6 answers

calling function with arguments in mustache javascript

Is it possible to call a function with arguments with Mustache.js {{somefunction(somevalue)}} thank you
sinisa
  • 869
  • 1
  • 6
  • 11
60
votes
9 answers

Can Mustache Templates do template extension?

I'm new to Mustache. Many templating languages (e.g., Django / Jinja) will let you extend a "parent" template like so... base.html {% block content %}{% endblock %} frontpage.html {% extends…
Chris W.
  • 37,583
  • 36
  • 99
  • 136
59
votes
6 answers

jQuery + client-side template = "Syntax error, unrecognized expression"

I just updated jQuery from 1.8.3 to 1.9, and it started crashing all of a sudden. This is my template: This is how I read it: modal_template_html =…
Evgeny
  • 6,261
  • 8
  • 35
  • 43
54
votes
2 answers

Handlebars Template rendering template as text

I created a helper in Handlebars to help with logic, but my template parses the returned html as text rather than html. I have a quiz results page that is rendered after the quiz is completed: