RE: Velocity and WebContent (CMS)

ma bu, modified 14 Years ago. Junior Member Posts: 31 Join Date: 5/14/10 Recent Posts
Hi there,

a few days ago I stumpled upon "Structures and Templates" for WebContents. It's a very nice feature but I don't really understand all of it.
I managed to create a structure and a corresponding template. Now I'm trying to enhance the Velocity Script in the template.
First of all: Is there anywhere a list of variables and functions which I can use here?
I found some lists but all these variables are only availible in themes and not in web content.(e.g. $user / $user_first_name )
Till now I managed to get the UserID with
$request.get("attributes").get("USER_ID")
but how can I get the real user name?

Next what I don't really understand is the following:
With
$groupId = $request.get("attributes").get("VIRTUAL_HOST_LAYOUT_SET").get("groupId")
it is possible to get the GroupID although
$myClass = $request.get("attributes").get("VIRTUAL_HOST_LAYOUT_SET").getClass()
returns "java.lang.String"

By trying to get a similar value with e.g.
$userId = $request.get("attributes").get("USER").get("userId")
the value isn't interpreted although
$myClass = $request.get("attributes").get("USER").getClass()
returns again "java.lang.String"

Why is it possible to access the first "String" like a HashMap, but not the second one?
Who can help me out?

Thanks in advance!
ma bu, modified 14 Years ago. Junior Member Posts: 31 Join Date: 5/14/10 Recent Posts
push
thumbnail
Deb Troxel, modified 14 Years ago. Junior Member Posts: 81 Join Date: 2/22/10 Recent Posts
ma bu:
Hi there,
First of all: Is there anywhere a list of variables and functions which I can use here?


Although this page is not current, you may find some useful information here http://www.liferay.com/community/wiki/-/wiki/Main/Journal+Template+%28Velocity%29
Instead of JournalVmUtil as mentioned on that page, refer to doTransform() in com.liferay.portlet.journal.util.VelocityTemplateParser.java
as well as insertHelperUtilities() in com.liferay.portal.velocity.VelocityVariables.java

If you use the request object or parameters in a template, make sure that you uncheck the "Cacheable' option.

Read through Ray Augé's old blogs for samples of what can be achieved using Web Content structures and templates, as well as accessing services.
www.liferay.com/web/raymond.auge/blog

ma bu:

Till now I managed to get the UserID with
$request.get("attributes").get("USER_ID")
but how can I get the real user name?

Try:
#set ($userLocalService = $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
#set ($userId = $getterUtil.getLong($request.get("attributes").get("USER_ID")))
#set ($user = $userLocalService.getUserById($userId))
$user.fullName

ma bu:

Next what I don't really understand is the following:
With
$groupId = $request.get("attributes").get("VIRTUAL_HOST_LAYOUT_SET").get("groupId")
it is possible to get the GroupID although
$myClass = $request.get("attributes").get("VIRTUAL_HOST_LAYOUT_SET").getClass()
returns "java.lang.String"

By trying to get a similar value with e.g.
$userId = $request.get("attributes").get("USER").get("userId")
the value isn't interpreted although
$myClass = $request.get("attributes").get("USER").getClass()
returns again "java.lang.String"

Why is it possible to access the first "String" like a HashMap, but not the second one?

Hmmm, are you completely sure that the code you posted is what you were using? I do not get $request.get("attributes").get("VIRTUAL_HOST_LAYOUT_SET").get("groupId") to return a groupId. AFAIK, all of the request "attributes" available in web content are String representations of the values stored in the request, not objects.

You can obtain the groupID directly as $groupId

Hope this helps.
- Deb
ma bu, modified 14 Years ago. Junior Member Posts: 31 Join Date: 5/14/10 Recent Posts
thanks for your reply, it helped me a lot.
But a problem remained: your code
#set ($userLocalService = $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
#set ($userId = $getterUtil.getLong($request.get("attributes").get("USER_ID")))
#set ($user = $userLocalService.getUserById($userId))
$user.fullName
doesn't work for me. I don't get any data.
Do I have to enable Velocity in any other 'properties' file? Maybe it's too easy but actually I only set
velocity.engine.resource.manager.cache.enabled=true
in developer-properties.
Cheers
Rubén García Tamayo, modified 14 Years ago. New Member Posts: 7 Join Date: 10/10/11 Recent Posts
I'm not sure if this solve your problem but it can help you.
Velocity in JournalArticle (Web Content) limit some variables to avoid develop complex tasks in template.
If you want to enable variables disabled by default, you would modify portal-ext.properties.

http://www.liferay.com/es/community/wiki/-/wiki/Main/Portal+Properties+6.0.5

#
# Input a comma delimited list of variables which are restricted from the
# context in Velocity based Journal templates.
#
journal.template.velocity.restricted.variables=serviceLocator
thumbnail
Deb Troxel, modified 14 Years ago. Junior Member Posts: 81 Join Date: 2/22/10 Recent Posts
Rubén is correct. Remove serviceLocator from the restricted list in your portal-ext.properties like this:

journal.template.velocity.restricted.variables=

Of course this opens up much more functionality to anyone with permissions to edit a template, so you would want to consider the security implications in a production environment.