janl / mustache.js

Minimal templating with {{mustaches}} in JavaScript
https://mustache.github.io
MIT License
16.48k stars 2.39k forks source link

adhock formatter - injected `render` handler ignores provided tag sindicator #605

Open osher opened 8 years ago

osher commented 8 years ago

Use case:

code generator.

Scenario (simplified)

Template:

{{=<% %>=}}
function foo() {
  var body = <%#indent%>
    <%{ body }%>
  <%/indent%>;
  return body
}

code:

var template = ... /* above */;
var model = {
  body: JSON.stringify( { foo: "bar" }, null, 2),
  indent: indent
}

indent.xLineBreaks  = /\r?\n/g;
function indent() { 
    return function(text, render) {
        var sep = /^\s+/.exec(text) || [''];
        sep = sep[0].substr(0, sep[0].length - 2);
        return render(text.trim()).replace(indent.xLineBreaks, sep)
    }  
}

console.log( mustache.render( template, model) );

Expected

function foo() {
  var body = {
    "foo": "bar" 
  };
  return body
}

Found

function foo() {
  var body =<%{ body }%>;
  return body
}

it works if I use for the inner expression {{{ / }}} instead <%{ / }%>, but that's not how the code is expected to work. It's effectively a hybrid form... The whole idea is a get away from the overload of {/} in the template text

osher commented 8 years ago

there was a bug in the example, I fixed it now. However - the bug report is still true