How to use velocity templates?

Hi.

I found many questions and answers about velocity templates in Confluence. And what about JIRA?

I have tried example from this page:

// Create the Velocity Context
Map context = MacroUtils.defaultVelocityContext();
context.put("myCustomVar", customVar);
context.put("otherCustomVar", otherCustomVar);
// Render the Template
String result = VelocityUtils.getRenderedTemplate("/com/myplugin/templates/macro.vm", context);

 but I cannot find appropriate packages for MacroUtils and VelocityUtils in JIRA.

What packages I need to user for it? Or does exists anything else way to use velocity templates in JIRA?

Thanks.

2 answers

This widget could not be displayed.

Hi Yanduganov,

You'll want to use the JIRA templates and JSP's guide for Velocity Templates in JIRA.  MacroUtils and VelocityUtils appear to be for Confluence per the Confluence Velocity template documentation and I was unable to find references to those methods in the JIRA docs.

Cheers,

Branden

Gaston Valente Community Champion Jul 26, 2017

great doc, saves a lot of time, thanks!

Hi Branden.

Your answer isn't about templates which I meant.

I write JIRA plugin and I have servlet. And I wanna render it using velocity template. I tryed write HTML just in java but I think that it's a wrong way.

This widget could not be displayed.

To use Velocity Template you need:

  1. add Scanned annotation to your class
  2. add TemplateRenderer field with ComponentImport annotation
  3. add TemplateRenderer to constructor
  4. use it

For example:

@Scanned
public class MyClass {
@ComponentImport
private final TemplateRenderer templateRenderer;

public MyClass(TemplateRenderer templateRenderer) {
this.templateRenderer = templateRenderer;
}
}

 After that you can use it, for example

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Map<String, Object> context = Maps.newHashMap();
resp.setContentType("text/html;charset=utf-8");

context.put("key", "value");

templateRenderer.render("path_to_vm_template", context, resp.getWriter());
}

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

292 views 5 0
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you