Velocity template dynamic changes

Hello there,

I’d like to know if there is a possibility to dynamically change a velocity template. I understood to get some server side object into vm file was to go with the context:

SourceObject sourceObject = ao.get(SourceObject.class,1);
Map<String, Object> context = new HashMap<String, Object>();

context.put(“source-object”,sourceObject);
res.setContentType(“text/html;charset=utf-8”);
templateRenderer.render(“templates/object-mapping.vm”, context, res.getWriter());

and access it like this in the vm file:

#foreach($field in $source-object.getSourceFields())
$field.getName()
#end

Which works pretty fine. Now I wanted to change the sourceObject ID (to get another object) through some javascript and render the changes again. So call the server side, pass the object id and render the template with the “new” context:

SourceObject sourceObject = ao.get(SourceObject.class,2);
Map<String, Object> context = new HashMap<String, Object>();

context.put(“source-object”,sourceObject);
res.setContentType(“text/html;charset=utf-8”);
templateRenderer.render(“templates/object-mapping.vm”, context, res.getWriter());

But it doesn’t seem to work. Are there any right “JIRA ways” how to change the velocity template dynamically?

Most likely you would be doing this via a form submission or AJAX call to a Webwork action/Servlet or REST endpoint that then renders a velocity template.

You can pass query parameters/data along with the AJAX call to your Java class (Webwork action/Servlet or REST) that’ll be rendering the velocity template.

In this Java class you can then have a method that passes different data to the template based on passed in params, or even render different velocity templates based on the parameters send by the JavaScript