I am writing velocity templates for my liferay theme and I am wondering, whether it is possible to create a new object inside the velocity template.
The reason is that in liferay there is no contextTool registered in the context and I really want to be able to inspect the variables that are present in the template at a given time. There is a cool macro for this, but unfortunately it uses the contexttool.
I'd like to do something like:
#set($contextTool = new ContextTool())
Another solution would be java code that is provided with the liferay theme that is able to add stuff in the template context. But I don't know how this would work either... ;-)
3.2 Create a Velocity Template Writer Initialize the Velocity engine. Read the template. Put the data model in a context object. Merge the template with context data and render the view.
Velocity is a server-side template language used by Confluence to render page content. Velocity allows Java objects to be called alongside standard HTML. If you are are writing a user macro or developing a plugin you may need to modify Velocity content.
Well, the reason is that the Velocity Engine has been deprecated for a while, and a lot of developers around the world need to find well-fitting alternatives.
try with
#set($contextTool = $portal.getClass().forName("full.package.ContextTool").newInstance())
EDIT
IF I understood you than this should give you what you want
#set($ve = $serviceLocator.findService("com.liferay.portal.kernel.velocity.VelocityEngine"))
#set($wvc = $ve.getWrappedStandardToolsContext().getWrappedVelocityContext())
#set($cVE = $portal.getClass().forName("org.apache.velocity.app.VelocityEngine"))
#set($cHSREQ = $portal.getClass().forName("javax.servlet.http.HttpServletRequest"))
#set($cHSRES = $portal.getClass().forName("javax.servlet.http.HttpServletResponse"))
#set($cSC = $portal.getClass().forName("javax.servlet.ServletContext"))
#set($cCC = $portal.getClass().forName("org.apache.velocity.tools.view.context.ChainedContext"))
#set($cVEI = $portal.getClass().forName("com.liferay.portal.velocity.VelocityEngineImpl"))
#set($cC = $portal.getClass().forName("org.apache.velocity.context.Context"))
#set($cVEU = $portal.getClass().forName("com.liferay.portal.kernel.velocity.VelocityEngineUtil"))
#set($ve = $cVEU.getMethod("getVelocityEngine").invoke(null))
#set($fVE = $cVEI.getDeclaredField("_velocityEngine"))
$fVE.setAccessible(true)
#set($cc = $cCC.getConstructor($cC, $cVE, $cHSREQ, $cHSRES, $cSC).newInstance($wvc, $fVE.get($ve), $request, $response, $request.getSession().getServletContext()))
#set($contextTool = $portal.getClass().forName("org.apache.velocity.tools.view.tools.ContextTool").newInstance())
$contextTool.init($cc)
After that you can use, for example
$contextTool.getKeys()
If this is not what you need, let me know ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With