Foros de discusión

template in creating web content

Abhi Ed, modificado hace 11 años.

template in creating web content

Regular Member Mensajes: 118 Fecha de incorporación: 4/06/12 Mensajes recientes
I am creating a web content in liferay. I have added a template for my structure.In template,i have chosen velocity as my script language.
I want to access logged in user's name in my vm script.
But when i use $user.getFirstName() as declared in init.vm it doesn't work, not even with $user_first_name as used in portal_normal.vm.
Can anyone help me?
thumbnail
Jignesh Vachhani, modificado hace 11 años.

RE: template in creating web content

Liferay Master Mensajes: 803 Fecha de incorporación: 10/03/08 Mensajes recientes
you can use below default velocity variables in web content :
http://www.liferaysolution.com/2011/10/default-velocity-variables-for-web.html .

But to get logged-in user details , you may have to call user service in template and need to set in separate variable using service locator.
And then you can use all the methods to get login user details.
Abhi Ed, modificado hace 11 años.

RE: template in creating web content

Regular Member Mensajes: 118 Fecha de incorporación: 4/06/12 Mensajes recientes
Jignesh Vachhani:
you can use below default velocity variables in web content :
http://www.liferaysolution.com/2011/10/default-velocity-variables-for-web.html .

But to get logged-in user details , you may have to call user service in template and need to set in separate variable using service locator.
And then you can use all the methods to get login user details.

Thanks for prompt reply.
But I don't know how to call user service in template.
I followed this link
http://www.liferay.com/community/wiki/-/wiki/Main/Access+Liferay+Services+in+Velocity#section-Access+Liferay+Services+in+Velocity-The+findService()+Method
but here there is no description for calling user service .
thumbnail
Jignesh Vachhani, modificado hace 11 años.

RE: template in creating web content

Liferay Master Mensajes: 803 Fecha de incorporación: 10/03/08 Mensajes recientes
you can use below code in your web content template :

#set($userLocalService = $serviceLocator.findExceptionSafeService("com.liferay.portal.service.UserLocalService"))

Now print $userLocalService and check what and all information you are getting.
Abhi Ed, modificado hace 11 años.

RE: template in creating web content

Regular Member Mensajes: 118 Fecha de incorporación: 4/06/12 Mensajes recientes
Jignesh Vachhani:
you can use below code in your web content template :

#set($userLocalService = $serviceLocator.findExceptionSafeService("com.liferay.portal.service.UserLocalService"))

Now print $userLocalService and check what and all information you are getting.



Hello,
This $userLocalService is not returning anything.I guess we need to use different service.Any idea?
thumbnail
Amit Doshi, modificado hace 11 años.

RE: template in creating web content

Liferay Master Mensajes: 550 Fecha de incorporación: 29/12/10 Mensajes recientes
Hi Abhi,

Please check below link. It is given with example.

http://liferaytrends.blogspot.in/2012/05/access-custom-portlet-service-in.html

Hope it helps.

Thanks & Regards,
Amit Doshi
thumbnail
Paul ., modificado hace 11 años.

RE: template in creating web content

Liferay Master Mensajes: 522 Fecha de incorporación: 29/08/11 Mensajes recientes
It's most probably because you have to allow it via portal-ext.properties.
This can be done by the property
 journal.template.velocity.restricted.variables=
leaving it blank will allow service locator to get the service
Abhi Ed, modificado hace 11 años.

RE: template in creating web content

Regular Member Mensajes: 118 Fecha de incorporación: 4/06/12 Mensajes recientes
Paul .:
It's most probably because you have to allow it via portal-ext.properties.
This can be done by the property
 journal.template.velocity.restricted.variables=
leaving it blank will allow service locator to get the service



Thanks!! I missed that.But the catch is, its still not working. My variable in template is still not getting set.I have set below mentioned variables.:
#set ($user = $userLocalService.getUser($request.get("theme-display").get("user-id")))
#set($userLocalService = $serviceLocator.findExceptionSafeService("com.liferay.portal.service.UserLocalService
Abhi Ed, modificado hace 11 años.

RE: template in creating web content

Regular Member Mensajes: 118 Fecha de incorporación: 4/06/12 Mensajes recientes
Hello,
I can access $userLocalService when i set it like below:

#set ($userLocalService= $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
Ican also access, $userCount by the setting : #set($userCount=$userLocalService.getUsersCount())

But , #set ($user = $userLocalService.getUserById($request.get("theme-display").get("user-id"))) here, $user is not accessible.

I have to get users first name.Any help???
Abhi Ed, modificado hace 11 años.

RE: template in creating web content

Regular Member Mensajes: 118 Fecha de incorporación: 4/06/12 Mensajes recientes
Finally, i got it.

For me the following lines worked :
#set ($userLocalService= $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))

#set ($userId = $getterUtil.getLong($request.remote-user))
#set($user = $userLocalService.getUserById($userId))
#set($userFirstName=$user.getFirstName())

Hope it helps,if someone searches the same.
thumbnail
Ben Chapman, modificado hace 11 años.

RE: template in creating web content

New Member Mensajes: 23 Fecha de incorporación: 8/03/11 Mensajes recientes
Thanks for all of this. This really helped me get this sorted out.

For others who might be searching and wanting to access expando content, here's an example of how to do that:


###
## Don't forget to set journal.template.velocity.restricted.variables=
## in portal-ext.properties; in other words, set it to nothing to unset the restriction.
###

## See http://www.liferay.com/community/forums/-/message_boards/message/14789299
## this is where this comes from

#set ($userLocalService= $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
#set ($userId = $getterUtil.getLong($request.remote-user))
#set($user = $userLocalService.getUserById($userId)) 

#set ($userFirstName=$user.getFirstName())
## This is a drop down select, so must be accessed this way
#set ($class_year = $user.getExpandoBridge().getAttribute("gradYear").get(0))
#set ($ugrad = $user.getExpandoBridge().getAttribute("UndergradSchool"))
#set ($userCount=$userLocalService.getUsersCount()) 

<h2>Getting variables from user, system, and expando</h2>
<p>Year: $class_year</p>
<p>School: $ugrad
</p><p>First Name: $userFirstName</p>
<p>User count: $userCount </p>