How to format html in plugin velocity template

Richard Heath March 22, 2012

I'm writing a plugin for a new Web Panel on the issues screen in Jira 5.0.1.

In that panel I pull a field in and try to display it. The field has embedded HTML and I want to format it.

The way to do this appears to be to use $textutils.htmlEncode(xxxx) method in the velocity template, but whatever I do the method does not appear to be called all I get in the output is $textutils.htmlEncode(xxxx) in the output.

If I just output the field contents (i.e. remove the outer htmlEncode method), it appears fine but without formatting.

Am I missing some way of "importing" $textutils into my plugin? I thought it should be in the default velocity context.

1 answer

0 votes
JamieA
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.
March 22, 2012

Only if you are calling super.getVelocityParameters in your getVelocityParameters method... are you?

You can also populate them with stuff like that by using: JiraVelocityUtils.getDefaultVelocityParams(authenticationContext)

Richard Heath March 22, 2012

I have no Java in my Plugin at all. All I have is the atlassian-plugin.xml file and the velocity template.

JamieA
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.
March 22, 2012

Can we see the section of the atlassian-plugin.xml that defines the new panel?

Richard Heath March 22, 2012

<web-panel name="XXX"

i18n-name-key="xxx.name"

key="xxx"

location="atl.jira.view.issue.right.context"

weight="0">

<description key="xxx.description">XXX</description>

<label>XXX</label>

<resource name="view" type="velocity" location="xxx.vm"/>

</web-panel>

JamieA
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.
March 22, 2012

Sorry, I'm not familiar with doing this without any code. It's possible that you just don't get textutils. Try putting this in your velocity to see what's available:

#foreach($p in $ctx.keySet().toArray())
  $p.toString() - $ctx.get($p).getClass().getName().toString()
#end

Richard Heath March 22, 2012

That returns absolutely nothing! Which is odd as I can access $i18n, $customFieldManager and $issue.

Richard Heath March 25, 2012

Ok, so in the end I wrote a context provider for my web panel that added an instance of the TextUtils class into the context.

public class ContextProvider extends AbstractJiraContextProvider
{
    public static final TextUtils textUtils = new TextUtils();
    public Map getContextMap(User user, JiraHelper jiraHelper) 

    {
        Map contextMap = new HashMap();
        contextMap.putAll(jiraHelper.getContextParams());
        contextMap.put("textUtils",textUtils);
        return contextMap;
    }
}

This then worked brilliantly on the atlas-run development system.

So I copied it across to the 5.0.1. test instance I have only to be presented with the following exception on every issue page:

Error rendering WebPanel (project-info.vm): java.lang.Exception: The specified class for ResourceManager (org.apache.velocity.runtime.resource.ResourceManagerImpl) does not implement org.apache.velocity.runtime.resource.ResourceManager; Velocity is not initialized correctly.

With a very, very long stack trace!

Any ideas?

Suggest an answer

Log in or Sign up to answer