Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Velocity Markup foreach range

James Star
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 16, 2012

Hello,

I am using the Velocity Markup foreach to retrieve a List of Spaces from the SpaceManager Class.

#set( $allSpaces = $spaceManager.getAllSpaces() )

#foreach($space in $allSpaces)

<b>$space.name</b><br/>

#end

But rather than retrieve a very long list of spaces, I would like to add a count OR range to the foreach.

I did notice that foreach has a range like - foreach($space in [1..3]) but this is not quite what I am looking for.

Has anyone else done something similar to this? Or have any ideas how to implement this.

Thank you,

James Star

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Andy Brook [Plugin People]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 20, 2012

If you're 'in' velocity, you can interact with the action that 'triggered' you to be parsed through $action (which is just an entry in the velocity context 'action' against the instance of the action, you can get access to $myVelocityHelper as well. Not sure I see the problem. The URL you give is an external one, the related Action class executes, and on success, through Xwork triggers the rendering phase, that may then interact with the '$action' to retrieve data as appropriate, or generally do things not best done in velocity.

Please restate the problem, or the point of confusion?

James Star
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 20, 2012

To confirm, there are two approaches that could be taken to access a method from a Java Class.

- Action Class OR

- Velocity Helper

Correct?

Another question: Can the Action Class be anything you define: e.g. myActionClass

Thanks Andy.

Andy Brook [Plugin People]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 21, 2012

I gues its possible to have an action without inheriting, it just usually doesnt work out useful to do so. An action class generally extends JiraWebActionSupport, inheriting useful stuff like getText(..) enabling i18n support through $action.getText('key'), and getLoggedInUser() etc.

1 vote
Andy Brook [Plugin People]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 16, 2012

Not sure I see the problem, unless you know what spaces you want, you need to get them all, go through them then decide one OR another, do do something with them (via a logical IF inside the foreach). Why would that not work?

There would be $velocityCount available, giving another route to counting, but JIRA 4.x is still using ancient Velcity 1.4, which is a shame as 1.5 brings support for loop counters (1.6 also now released), which may help: http://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html#loops

## list first 5 customers only

#foreach( $customer in $customerList )
    #if( $velocityCount > 5 )
        #break
    #end
    $customer.Name
#end

Get voting on for https://jira.atlassian.com/browse/JRA-12121

James Star
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 16, 2012

Hmm, I did consider this, but if there are like hundreds of spaces then it could be quite time consuming.

Another thing I came across was SpacesQuery but I am not sure if this can be used inside Macro User or only inside Velocity Templates?? Any thoughts.

Andy Brook [Plugin People]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 16, 2012

Given they are loaded every time the dashboard gets hit, they will be heavily cached. If you able to filter based on any specific criteria, Alains suggestion is the solution, all others are deprecated, just build up the SpacesQuery http://docs.atlassian.com/atlassian-confluence/latest/com/atlassian/confluence/spaces/SpacesQuery.html to suite your needs, test it in a usermacro to see what issues you run into.

I think I see the problem, velocity cant access a static class (in this case SpacesQuery.Builder.build(), unless the base classits been injected into your context, see http://stackoverflow.com/questions/462223/how-to-access-static-members-in-a-velocity-template Guessing your next question is how to contribute to the VelocityContext. You only get https://developer.atlassian.com/display/CONFDEV/Confluence+Objects+Accessible+From+Velocity , you can probably fix that by contributing to the default context - https://developer.atlassian.com/display/CONFDEV/Velocity+Context+Module

James Star
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 16, 2012

Hi Andy,
thanks for the reply back. I will review all links and come back shortly :)
Cheers!

James Star
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 19, 2012

Hi Andy,

have reviewed all links.

Just trying something hopefully relatively simple to start with.

In my main.vmd I am making a call with $action.myClass

And in my Java file, I have a method getMyClass(), which returns a String helloVM

I'm guessing this should display the actual string in the published HTML, but I am just setting the "$action.myClass".

The other quesiton I have is how does Confluence link or trigger the Java files?

Thank you,

James Star

James Star
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 19, 2012

Okay, also noticed my Java Class wasn't properly instantiated, have done this.

Added a module into my atlassian-plugin.xml.

But this still hasn't resolved the previous issue, testing a simple action.

0 votes
Andy Brook [Plugin People]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 19, 2012

That last comment paints a different picture. If you have an action driving your template you're done, just implement a public method doSomething(), then $action.doSomething(). You dont need any of the above stuff, unless you want to access something _in_ velocity when you _arent_ driven by an action under your control. Bottom line, if its not presentational, do it in a java helper method.

James Star
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 19, 2012

Hi Andy, I have something working. I set up a Velocity Helper and then referenced this in-order to call a method.

E.g. myVelocityHelper.callThis()

But this seems different from what else I have been reading, $action.doSomething() which seems to be refenced around XWork? where you can also call this using - http://localhost:1990/confluence/plugins/livesearch/livesearch.action.

Thanks Andy, look forward to your reply.

TAGS
AUG Leaders

Atlassian Community Events