handlebars.java icon indicating copy to clipboard operation
handlebars.java copied to clipboard

Making the Handlebars class "engine" more open

Open edouardmercier opened this issue 4 years ago • 0 comments

In order to use the compiler "compat" option, I needed to:

  1. access the Handlerbars.engine() method which is private ;
  2. execute the following code, so as to provide the options argument:
javaScript = Throwing.get(() -> {
        final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
        Throwing.run(() -> {
          engine.eval(Files.read("/handlebars.js", Charset.forName("UTF-8")));
        });
        final String handlebarsName = "Handlebars";
        final String templateName = "template";
        final String optionsName = "options";
        final Object handlebarsObject = engine.getContext().getAttribute(handlebarsName);
        final Bindings bindings = engine.createBindings();
        bindings.put(handlebarsName, handlebarsObject);
        bindings.put(templateName, input);
        final Map<String, Object> options = new HashMap<String, Object>();
        options.put("compat", true);
        options.put("strict", false);
        options.put("assumeObjects", false);
        bindings.put(optionsName, options);
        return (String) engine.eval("Handlebars.precompile(" + templateName + ", " + optionsName + ");", bindings);
      });

where input in the template code.

It would be nice to be able to perform this without having to copy-and-paste Handlebars Java source code. I propose to turn the Handlerbars.engine() method public, and to offer an additional method which takes the options as a parameter.

What do you think of this?

edouardmercier avatar Nov 06 '19 09:11 edouardmercier