mustachemustache.php

How to use mustache php to render unadulterated mustache js template with curly braces intact


I want to render a javascript mustache template in mustache php. How do I render {{foo}} inside of script tags from php mustache with the {{foo}} curly braces being escaped, so that I can later render it as mustache.js template?


Solution

  • I figured it out. I replaced all double curly braces {{var}} with angle brackets + percentage signs <%var%> in the php mustache template. That way the delimiters persist after the php mustache rendering. Then to render with mustache.js I added a couple of lines between the template call and render to set custom delimiters and parse template.

    var template = $("#mytemplate").html();
    var customTags = [ '<%', '%>' ];
    Mustache.parse(template, customTags);
    var output = Mustache.render(template, data);