cancel
Showing results for 
Search instead for 
Did you mean: 

Process template from java backed webscript

dreambitc
Champ in-the-making
Champ in-the-making
Hi all!

I have java backed webscript and i need to process FreeMarker template file against the node in Java, using JavaScript it can be achieved using the following code:

document.processTemplate(template)‍‍‍


How can i do the same thing in Java? I've tried create ScriptNode manually and call processTemplate method but i don't have scope object and when i call processTemplate i have NullPointerException.

Is there some solutions to solve it?

Thanks in advance!
2 REPLIES 2

mitpatoliya
Star Collaborator
Star Collaborator
You can use TemplateService APIs to achieve same.
public void processTemplateString(String engine,
                         String template,
                         Object model,
                         Writer out)
                           throws TemplateException

dreambitc
Champ in-the-making
Champ in-the-making
Sorry for such a long reply, thx <b>mitpatoliya</b> it works.
if anyone is interested, here is example of usage:

TemplateService templateService = serviceRegistry.getTemplateService();// our modelMap<String, Object> model = new HashMap<String, Object>(8, 1.0f);// modelNodeRef is the node where from we are gonna take valuesmodel.put("document", new TemplateNode(modelNodeRef, serviceRegistry, null));// TemplateNode allows go get node properties, assocs etc.‍‍‍‍‍‍‍‍


and also template example

<#assign props=document.getProperties()>${props["cm:name"]‍‍‍‍