liferayvelocityliferay-themeliferay-velocity

Access Liferay Global Velocity Variable in Custom Portlet


As of now, Liferay provides a set of global variables as defined in init.vm that can be used by theme templates. These variables are set in init.vm and is located inside Liferay ROOT.war : $PATH_TO_WEBAPPS/ROOT/html/themes/_unstyled/templates.

And Inside my custom theme portal_normal.vm, I could use it by simply adding #parse ($init). Which works great.

Now moving into my problem. I am using velocity template for sending my email address.So, Inside my portlet i have created an email template in the location as $SOME_PORTLET_NAME/src/main/webapps/template/email.vm. I could send the custom variables to the template using,

VelocityContext velocityContext = new VelocityContext();
velocityContext.put("vp", velocityParameters);

But I couldn't use the default variables provided by the Liferay as used in theme.

I am trying to use $languageUtil.get for internationalization in my email template.

What have i tried?

Question :

Thanks!!


Solution

  • It might not be the full answer but a pointer where to start: You're creating a blank VelocityContext. This is naturally empty - no surprise there.

    When Liferay executes its init.vm and other templates, it doesn't run on a blank context (though init.vm seems very basic). Depending on your version of Liferay, look up VelocityVariablesImpl or TemplateContextHelper for the initialization of a scripting context.

    Note that this all lives in portal-impl, thus in Liferay's implementation which you typically don't have access to. You might need to duplicate the parts of the code that you're interested in as you typically can't call them directly. But then, you probably don't need the full initialization as well.

    If this helps you find an actual solution, please post some hints on what you actually settled with (or a link to it)