Convert Spin to ScriptObjectMirror for parsing with freemarker

Hi,

Following a previous topic from @StephenOTT (Freemarker Script: Greater than 4000 char, exceeding historic variable instance DB insert - #7 by StephenOTT) I’ve been able to reproduce:

var placeholderValues= {
“firstName”: “Someones First Name”,
“id”: “123456789”,
“time”: “5”,
“name”: “A Name”
}
// Render the html into the freemarker engine
var ScriptEngine= new JavaImporter(javax.script);
var emailContent = “”;
with (ScriptEngine) {
var manager = new ScriptEngineManager();
var engine = manager.getEngineByName(“freemarker”);
var bindings = engine.createBindings();
bindings.put(“placeholders”, placeholderValues);
var rendered = engine.eval(content, bindings);
emailContent = rendered;
};

But what if my placeholderValues is a SPIN object (create before the parsing task)? How can I make it work? i.e. how to convert the SPIN object into ScriptObjectMirror?

Thanks,

Ch.

You do something like:

...
var myJson = execution.getVariable('mySpinVar')
...
bindings.put('placeholders', myJson)

A SPIN variable is just a typical Java object that wraps JSON. So when you pass the value into freemarker it should be available as a java object that you can interact with as a SPIN java object.

On related note here are some good docs about some of the more adv features of nashorn: https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions. It notes that nashorn provides the interop with the java objects, so you dont (most of the time) need to worry about ScriptObjectMirror.

Hi @StephenOTT, This was my first attempt and I get the following:

The process could not be started. : javax.script.ScriptException: freemarker.core.InvalidReferenceException: The following has evaluated to null or missing: ==> placeholders.receiver_firstname [in template “unknown” at line 1, column 8] ---- Tip: It’s the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to legally refer to something that’s sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace (“~” means nesting-related): - Failed at: ${placeholders.receiver_firstname} [in template “unknown” at line 1, column 6] ----

After some reading, the workaround is to convert my spin variable using:

placeholder_values = JSON.parse(spin_var.toString());

This way it can be read by bindings.put

You are not interacting with your placeholders as a Java Object. When you access the SPIN object in the freemarker you need to use the SPIN java api because you are interacting with a SPIN java object.

(i have never tested SPIN in freemarker, so could be some other issues)

I do agree, the best would be to use a SPIN object directly in freemarker