Foros de discusión

Freemarker template - access to themeDisplay

thumbnail
Bartlomiej Knabel, modificado hace 9 años.

Freemarker template - access to themeDisplay

Junior Member Mensajes: 76 Fecha de incorporación: 30/01/11 Mensajes recientes
hi,

Is there any way to access ThemeDisplay object in normal freemarker template (not ADT).
Yes, I know - there is a method like ${request['theme-display']}, but it doesn't return real object (it is simple hash array)

best regards
thumbnail
James Falkner, modificado hace 9 años.

RE: Freemarker template - access to themeDisplay

Liferay Legend Mensajes: 1399 Fecha de incorporación: 17/09/10 Mensajes recientes
Bartlomiej Knabel:
hi,

Is there any way to access ThemeDisplay object in normal freemarker template (not ADT).
Yes, I know - there is a method like ${request['theme-display']}, but it doesn't return real object (it is simple hash array)

best regards


Unfortunately not, however you can use a trick to create a new instance and populate it with enough data to be able to use it as a real ThemeDisplay. For example, here's some code I use in a WCM template to construct a ThemeDisplay object so I can call the User.getPortraitURL() method:

<#assign portalURL = httpUtil.getProtocol(request['attributes']['CURRENT_URL']) + "://" + getterUtil.getString(request['theme-display']['portal-url']) />
<#assign mainPath = getterUtil.getString(request['theme-display']['path-main']) />

<#assign themeDisplay = objectUtil("com.liferay.portal.theme.ThemeDisplay") />
<#assign V = themeDisplay.setPathImage(getterUtil.getString(request['theme-display']['path-image'])) />
<#assign V = themeDisplay.setPathMain(getterUtil.getString(request['theme-display']['path-main'])) />
<#assign V = themeDisplay.setPortalURL(portalURL) />
<#assign V = themeDisplay.setPermissionChecker(permissionChecker) />
<#assign V = themeDisplay.setScopeGroupId(getterUtil.getLong(request['theme-display']['scope-group-id'])) />

<#assign userService = utilLocator.findUtil("com.liferay.portal.service.UserLocalService") />
<#assign user = userService.fetchUserById(getterUtil.getLong(request['theme-display']['user-id'])) />

<#assign profileUrl = user.getDisplayURL(portalURL, mainPath) />
<#assign profilePic = user.getPortraitURL(themeDisplay) />

<a href="${profileUrl}">
  <img alt="${htmlUtil.escape(user.getFullName())}" src="${profilePic}">
</a>
thumbnail
Krzysztof Gołębiowski, modificado hace 9 años.

RE: Freemarker template - access to themeDisplay

Liferay Master Mensajes: 549 Fecha de incorporación: 25/06/11 Mensajes recientes
Hi Bartłomiej and James,
I've had this issue some time ago - I don't remember what exaclty was the problem, I think we had a FOOTER article embedded using journalContentUtil and needed to use some API which required full themeDisplay/request objects. I was playing with this case for some time and finished with EXTed com.liferay.portal.templateparser.Transformer class where I had to add the following lines to transform method:
template.put("themeDisplayForWc", themeDisplay);

It is called themeDisplayForWc because as far as I remember adding new bean with key themeDisplay caused some strange problems. This solution seemed to work fine but it was so ugly and unmanageable that I decided to implement the whole functionality in different way emoticon

Regards,
KG
thumbnail
James Falkner, modificado hace 9 años.

RE: Freemarker template - access to themeDisplay

Liferay Legend Mensajes: 1399 Fecha de incorporación: 17/09/10 Mensajes recientes
Krzysztof Gołębiowski:
Hi Bartłomiej and James,
I've had this issue some time ago - I don't remember what exaclty was the problem, I think we had a FOOTER article embedded using journalContentUtil and needed to use some API which required full themeDisplay/request objects. I was playing with this case for some time and finished with EXTed com.liferay.portal.templateparser.Transformer class where I had to add the following lines to transform method:
template.put("themeDisplayForWc", themeDisplay);

It is called themeDisplayForWc because as far as I remember adding new bean with key themeDisplay caused some strange problems. This solution seemed to work fine but it was so ugly and unmanageable that I decided to implement the whole functionality in different way emoticon

Regards,
KG


Heh, nice emoticon According to the now-deprecated wiki page, "The processing of a template is handled in such a way that it is not dependent on access to a real HttpRequest of any kind. Why? Because it's done WITHIN the service call. We do not allow our service layer objects to have an HttpRequest or HttpResponse of any kind as parameters. Thus, we can't add any of those NON-serializable objects like ThemeDisplay"
thumbnail
Alessandro Lachina, modificado hace 8 años.

RE: Freemarker template - access to themeDisplay

New Member Mensajes: 24 Fecha de incorporación: 12/06/13 Mensajes recientes
It work for me and themeDisplay is accessible.
But in catalina log I found this error:

Expression request['theme-display'] is undefined on line ..


Any idea on how to avoid it?
Yeray Rodríguez, modificado hace 8 años.

RE: Freemarker template - access to themeDisplay

New Member Mensajes: 5 Fecha de incorporación: 2/03/15 Mensajes recientes
Alessandro Lachina:
It work for me and themeDisplay is accessible.
But in catalina log I found this error:

Expression request['theme-display'] is undefined on line ..


Any idea on how to avoid it?



Hi Alessandro,
I had the same problema. I find out that Liferay processes the template twice, the second one to get a summary of the content. The main difference is the second time Liferay doesn't pass the request object. I think the reason is the summary is not related to a real page.
To avoid the log error check request object has content.

&lt;#if .vars['request']?has_content&gt;
&lt;#-- code which use request objects --&gt;
For instance: ${request.secure}
<!--#if-->
Traolly Xiong, modificado hace 8 años.

RE: Freemarker template - access to themeDisplay

Regular Member Mensajes: 195 Fecha de incorporación: 30/12/11 Mensajes recientes
Hello Yeray,
I am getting the same error, but this occurs on the staging site page.

Expression request['theme-display'] is undefined on line 42, column 62 in 10154#10192#4762100.

The object works fine on my local machine, but not on our DEV server.

Any thoughts?

Thanks.
Igor Kanshyn, modificado hace 7 años.

RE: Freemarker template - access to themeDisplay

New Member Mensajes: 4 Fecha de incorporación: 21/03/12 Mensajes recientes
Hi,

I'm trying to use
&lt;#assign portalURL = httpUtil.getProtocol(request['attributes']['CURRENT_URL']) + "://" + getterUtil.getString(request['theme-display']['portal-url']) /&gt;

but getting error:
request['attributes'] is undefined
I'm in web content FreeMarker template on Liferay 6.2

Any ideas why request['attributes'] is not available for me?
Shubhankit Roy, modificado hace 7 años.

RE: Freemarker template - access to themeDisplay

New Member Mensaje: 1 Fecha de incorporación: 29/12/14 Mensajes recientes
Try using this:


&lt;#assign serviceContext = staticUtil["com.liferay.portal.service.ServiceContextThreadLocal"].getServiceContext()&gt;
&lt;#assign themeDisplay = serviceContext.getThemeDisplay() /&gt;
thumbnail
Alessandro Lachina, modificado hace 7 años.

RE: Freemarker template - access to themeDisplay

New Member Mensajes: 24 Fecha de incorporación: 12/06/13 Mensajes recientes
great! it works
thumbnail
Alessandro Candini, modificado hace 6 años.

RE: Freemarker template - access to themeDisplay

Regular Member Mensajes: 130 Fecha de incorporación: 17/10/15 Mensajes recientes
Shubhankit Roy:
Try using this:


&lt;#assign serviceContext = staticUtil["com.liferay.portal.service.ServiceContextThreadLocal"].getServiceContext()&gt;
&lt;#assign themeDisplay = serviceContext.getThemeDisplay() /&gt;


This is the way, with Liferay 7 too!

Thank you so much!
thumbnail
Massimo Bevilacqua, modificado hace 6 años.

RE: Freemarker template - access to themeDisplay

Regular Member Mensajes: 210 Fecha de incorporación: 27/12/16 Mensajes recientes
In liferay 7 the class is:

com.liferay.portal.kernel.service.ServiceContextThreadLocal

and not:

com.liferay.portal.service.ServiceContextThreadLocal
thumbnail
Achmed Tyrannus Albab, modificado hace 6 años.

RE: Freemarker template - access to themeDisplay

Regular Member Mensajes: 158 Fecha de incorporación: 5/03/10 Mensajes recientes
Shubhankit Roy:
Try using this:


&lt;#assign serviceContext = staticUtil["com.liferay.portal.service.ServiceContextThreadLocal"].getServiceContext()&gt;
&lt;#assign themeDisplay = serviceContext.getThemeDisplay() /&gt;


<#assign imagesPath = getterUtil.getString(request['theme-display']['path-theme-images']) />
${imagesPath} gets me /theme-directory/image-directory .

<#assign mainPath = getterUtil.getString(request['theme-display']['path-main']) />
${mainPath} gets me com.liferay.portal.theme.ThemeDisplay@4d91e899

which is the same with what youve provided.

What i want is just /theme-directory/ and thats it.
How do i get this?