0

My mustache template is not rendering and I don't understand why not.

var output = Mustache.render('{{#languages}} {{@index}} {{locale}} {{/languages}}', '{"languages" : [ { "locale" : "english", "code" : "en" }, { "locale" : "deutsch", "code" : "de" }, { "locale" : "español", "code" : "es" }, { "locale" : "français", "code" : "fr" } ]}');

This output is empty. I also want the index to be rendered with each item.

Demo

mynameisnotallowed
  • 564
  • 1
  • 11
  • 34

3 Answers3

3

The first issue is that you should pass an Object as your data, not a string. Remove the quotes from around your languages object.

The second issue is that Mustache does not support the {{@index}} special reference. Many Mustache-like templating systems such as Dust.js and Handlebars.js do intrinsically support this, but in Mustache you'll have to write a helper and include it in your data. To do that, see one of the many other StackOverflow answers on the topic, such as this one.

Community
  • 1
  • 1
Interrobang
  • 16,984
  • 3
  • 55
  • 63
1

For one thing you're passing a string instead of an object as the 2nd argument.

JMM
  • 26,019
  • 3
  • 50
  • 55
1

Second parameter in Mustache.render() is object. you have passed a string

Mustache.render('{{#languages}} {{@index}} {{locale}} {{/languages}}',  {"languages" : [ { "locale" : "english", "code" : "en" }, { "locale" : "deutsch", "code" : "de" }, { "locale" : "español", "code" : "es" }, { "locale" : "français", "code" : "fr" }, { "locale" : "italiano", "code" : "it" }, { "locale" : "português (Portugal)", "code" : "ptpt" }, { "locale" : "português (Brasil)", "code" : "prbr" }, { "locale" : "Norsk", "code" : "no" }, { "locale" : "日本語", "code" : "jp" }, { "locale" : "中文", "code" : "cn" }, { "locale" : "Русский язык", "code" : "ru" } ]} );
theAnubhav
  • 535
  • 6
  • 16