Forums de discussion

Freemarker template - access to themeDisplay

thumbnail
Bartlomiej Knabel, modifié il y a 9 années.

Freemarker template - access to themeDisplay

Junior Member Publications: 76 Date d'inscription: 30/01/11 Publications récentes
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, modifié il y a 9 années.

RE: Freemarker template - access to themeDisplay

Liferay Legend Publications: 1399 Date d'inscription: 17/09/10 Publications récentes
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, modifié il y a 9 années.

RE: Freemarker template - access to themeDisplay

Liferay Master Publications: 549 Date d'inscription: 25/06/11 Publications récentes
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, modifié il y a 9 années.

RE: Freemarker template - access to themeDisplay

Liferay Legend Publications: 1399 Date d'inscription: 17/09/10 Publications récentes
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, modifié il y a 8 années.

RE: Freemarker template - access to themeDisplay

New Member Publications: 24 Date d'inscription: 12/06/13 Publications récentes
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, modifié il y a 8 années.

RE: Freemarker template - access to themeDisplay

New Member Publications: 5 Date d'inscription: 02/03/15 Publications récentes
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, modifié il y a 8 années.

RE: Freemarker template - access to themeDisplay

Regular Member Publications: 195 Date d'inscription: 30/12/11 Publications récentes
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, modifié il y a 7 années.

RE: Freemarker template - access to themeDisplay

New Member Publications: 4 Date d'inscription: 21/03/12 Publications récentes
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, modifié il y a 7 années.

RE: Freemarker template - access to themeDisplay

New Member Envoyer: 1 Date d'inscription: 29/12/14 Publications récentes
Try using this:


&lt;#assign serviceContext = staticUtil["com.liferay.portal.service.ServiceContextThreadLocal"].getServiceContext()&gt;
&lt;#assign themeDisplay = serviceContext.getThemeDisplay() /&gt;
thumbnail
Alessandro Lachina, modifié il y a 7 années.

RE: Freemarker template - access to themeDisplay

New Member Publications: 24 Date d'inscription: 12/06/13 Publications récentes
great! it works
thumbnail
Alessandro Candini, modifié il y a 6 années.

RE: Freemarker template - access to themeDisplay

Regular Member Publications: 130 Date d'inscription: 17/10/15 Publications récentes
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, modifié il y a 6 années.

RE: Freemarker template - access to themeDisplay

Regular Member Publications: 210 Date d'inscription: 27/12/16 Publications récentes
In liferay 7 the class is:

com.liferay.portal.kernel.service.ServiceContextThreadLocal

and not:

com.liferay.portal.service.ServiceContextThreadLocal
thumbnail
Achmed Tyrannus Albab, modifié il y a 6 années.

RE: Freemarker template - access to themeDisplay

Regular Member Publications: 158 Date d'inscription: 05/03/10 Publications récentes
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?