Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

JIRA custom plugin : How to pass variables from java class to vm template ?

How can I pass a variable (e.g: String myvar= "Hello World";) from my java class to my template file called test.vm ?

I was following the Admin UI tutorial, but that part is not very clear, or not covered.

At this moment I have the following code :

String myvar = "Hello World";
context.put("myvar",myvar);
response.setContentType("text/html;charset=utf-8");
templateRenderer.render("test.vm", response.getWriter());

Please also provide the code which belongs in the *.vm file. I'm assuming you use $myvar ?

Kind regards
Karel.

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted

Hi,

I think that you have omitted the context in the render method

try this:

String myvar = "Hello World";
context.put("myvar",myvar);
response.setContentType("text/html;charset=utf-8");
templateRenderer.render("test.vm",context, response.getWriter());

0 votes

You've answered your own question. The velocity variably $myvar can be used in the .vm file. It's really just $myvar - as you've got a string, it'll be able to parse that straight out into text.

Well I must be missing something, because at the moment is it not working. Instead of printing "Hello World" it is printing '$myvar'. So the code is not seen as a variable, but as text.

Regards.

How did you solve it? What sort of object is "context"? Do you have a full code example?

Dont suppose you found what object context is?

Here is some sample code showing how to pass variables into a velocity template in an HttpServlet (in this case our variables control whether or not to show an OAuth button on a settings page):

Integer OAuthed = 0;
Map<String, Object> context = new HashMap<String, Object>();
context.put("OAuthed", OAuthed);
renderer.render("admin.vm", context, response.getWriter());

 We use the variables in the velocity template file (ours was called admin.vm) like this:

#if ($OAuthed == 1)
<a href="/jira/plugins/servlet/test/admin?clearOAuth=1">Clear OAuth</a>
#end

You also need to use these dependencies in your HttpServlet:

import java.util.HashMap;
import java.util.Map;

That's spectacular. Thanks for the help!

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you