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>
1870 questions
351
votes
8 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…
277
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
  • 21,108
  • 30
  • 115
  • 171
218
votes
8 answers

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

As the title of question says, 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…
Ben
  • 19,033
  • 11
  • 65
  • 105
111
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
  • 8,519
  • 6
  • 30
  • 33
104
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
  • 66,562
  • 63
  • 166
  • 223
89
votes
5 answers

Is it possible to have nested templates in Go using the standard library?

How do I get nested templates like Jinja has in the python runtime. TBC what I mean is how do I have a bunch of templates inherit from a base templates, just filing in blocks of the base templates, like Jinja/django-templates does. Is it possible…
Sri Kadimisetty
  • 1,684
  • 3
  • 20
  • 25
87
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,211
  • 4
  • 21
  • 18
68
votes
4 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
  • 97,752
  • 174
  • 583
  • 971
66
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,169
  • 16
  • 50
  • 79
60
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
  • 859
  • 1
  • 6
  • 11
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,071
  • 8
  • 33
  • 42
58
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,699
  • 1
  • 32
  • 48
56
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.
  • 32,518
  • 30
  • 89
  • 121
52
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: