Render Velocity template in Java Component

Hi,

I’m startet writing my first UIExtension Java Component and have problems to return its execute result as a rendered Velocity template:

\src\main\resources\templates\view.vm <- Velocity template with test content

@Override
public Block execute() {
  return templateManager.renderNoException(“templates/view.vm”);
}

I tested the code above but templateManager.renderNoException returns an empty string without any exception or indication what the problem is.

Question 1: Is this the official way to use a velocity template within a Java component?

Question 2: How do I inject Java variable values to use them in the velocity template?

b.R.

UPDATE: I think the problem is, that templateManager can not find velocity templates located IN the jar out-of-the-box.

Well, it definitely can, and this is used a lot.

Your problem is more the path you are indicateding: as suggested by https://extensions.xwiki.org/xwiki/bin/view/Extension/Template%20Module#HClassloadertemplates, template files are searched in templates/, so when you ask for “templates/view.vm, it’s trying to find templates/templates/view.vm.

Here is an example with a very similar use case:

You can inject script binding (for any script language) using the ScriptContext, and you can access the current ScriptContext using ScriptContextManager component. See https://extensions.xwiki.org/xwiki/bin/view/Extension/Script%20Module/ for more details.