title: Lodash template engine Nashorn
body: <p>I'm currently trying to use the lodash template engine in Java using nashorn and I'm facing an issue.</p>
  
  <p>Here's the code : </p>
  
  <pre><code>ScriptEngineManager mgr = new ScriptEngineManager();
  ScriptEngine nashorn = mgr.getEngineByName("nashorn");
  ScriptContext ctx = nashorn.getContext();
  Bindings bindings = ctx.getBindings(ScriptContext.ENGINE_SCOPE);
  nashorn.eval(readFileToString("externallibs/lodash.min.js"), bindings);
  String tpl = readFileToString("hello.tpl");
  bindings.put("tpl", tpl);
  nashorn.eval("compiled = _.template(tpl);", bindings);
  ScriptObjectMirror compiled = (ScriptObjectMirror)nashorn.get("compiled");
  System.out.println("compiled = " + compiled);
  Map&lt;String, String&gt; props = new HashMap&lt;String, String(1);
  props.put("name", "world");
  bindings.put("props", props);
  System.out.println(compiled.call(this, bindings.get("props"));
  </code></pre>
  
  <p>The template is well compiled by Nashorn, if you look at the console you'll see :</p>
  
  <pre><code>compiled = function(obj){obj||(obj={});var __t,__p='';with(obj){__p+='Hello, '+((__t=( name ))==null?'':__t);}return __p}
  </code></pre>
  
  <p>But when I'm trying to call the compiled template function above with a map as parameter (the props), as you would do in JS : 
  <code>
  tpl.call(this, {name:'world'})
  </code></p>
  
  <p>It fails with the following error : </p>
  
  <p><code>
  TypeError: Cannot apply "with" to non script object
  </code></p>
  
  <p>And indeed, we can see that the compiled version of the function uses the 'with' keyword.</p>
  
  <p>Does anyone have an idea on how to workaround this issue ? How should I send the parameters to render the compiled template ?</p>
  
  <p>Thanks a lot for any help.</p>