Howto get WebItemModuleDescriptor in Velocity Template?

Within a Web Item I pass a parameter to my Velocity Template as it is described here within the “Param Elements” section

There you are told that:

The value can be retrieved from within the Velocity view with the following code, where $item is a WebItemModuleDescriptor:


 $item.webParams.get("key") <!-- retrieve the value -->
 $item.webParams.getRenderedParam("key", $user, $helper) <!-- retrieve the Velocity rendered value -->

But how to create the $item variable within the velocity template? Or how do I create a reference of this WebItemModuleDescriptor?

You can get the WebItemModuleDescriptor like this:

    String moduleKey = "com.projectbalm.riskregister.riskregister-jira:admin-link-settings";
    ModuleDescriptor<?> descriptor = pluginAccessor.getEnabledPluginModule(moduleKey);
    WebItemModuleDescriptor item = (WebItemModuleDescriptor) descriptor;

The module key has to be fully qualified; so in the above example, the plugin key is com.projectbalm.riskregister.riskregister-jira and the module key is admin-link-settings. You just join 'em together with a colon.

Then you can pass the item variable into your template as you would any other object reference.

I’m struggling with this myself, and a key part of my question includes “when rendering a velocity template using a ‘resource’ descriptor of type ‘velocity’”.

In this case the answer provided by @david.pinn doesn’t apply because we’re not writing any code. Surely we can get to our parameters from the template without writing code?

There might be a small problem with the documentation; clearly, it suggests that $item item is defined for use in the template, yet the Contents of the Velocity Context doesn’t include the WebItemModuleDescriptor.

Have you tried to access $item?

Thanks for responding @david.pinn

I just tried to access $item and it is rendered without any replacement (ie, the finally output page contains “${item}”). The same happens when I attempt to access $customFieldManager, which is always available according to Latest updates

I don’t really know where to go from here, so any suggestions you can provide would be most welcomed :slight_smile:

My workaround is to piggy-back of the i18n system. Since we’re only after strings here, I’ve convinced myself this isn’t too dirty :slight_smile:

In your properties files

not.hard.coded = something pretty simple

In your template

<script>
  var v = '${i18n.getText("not.hard.coded")}';
</script>

The downside of this approach is that you need to define it for every language if you’ve got a multi-lang plugin. Alternatively you could create an Admin Configuration Form but that seems a bit over the top for access to key/value pairs.

I also had an idea about how to see what’s available in the velocity context, which was to create a custom renderer and inspect the map that’s passed to the render method. Again, too much work for me ATM just to get to some key/value pairs :slight_smile:

Actually, the admin page is really the correct solution as the point of having these params is that they can change :slight_smile: