Search code examples
javascriptmustache

Mustache nested templates


I am wondering if this is possible. In this example the HTML file would be:

{{i18n.sample_message}}

in my render function I have this:

var json = {
 i18n:i18n,
 sampleDate:'10/10/10'
}
$('div').html(Mustache.to_html(template,json);

The i18n file is an object and would have a key:

sample_message: some long message
date is: {{json.sampleDate}}

right now I get {{json.sampleDate}} on the screen. I have tried ending the string at the semicolon and using + to concatenate the value but that did not work either.

For the time being I am not putting {{json.sampleDate}} in the i18n map I changed my html to

{{i18n.sample_message}}{{json.sampleDate}}

In reality I have some long paragraphs that I need to inject some dynamic values into.


Solution

  • I was able to get this working in an ugly way. Please comment/edit if there is something cleaner/better.

    I had to call Mustache.to_html twice.

    var html = Mustache.to_html(template,json);
    return Mustache.to_html(html,json);
    

    By calling to_html again, Mustache found {{json.sampleDate}} and replaced it with the value in my json.