掲示板

Freemarker template - access to themeDisplay

thumbnail
9年前 に Bartlomiej Knabel によって更新されました。

Freemarker template - access to themeDisplay

Junior Member 投稿: 76 参加年月日: 11/01/30 最新の投稿
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
9年前 に James Falkner によって更新されました。

RE: Freemarker template - access to themeDisplay

Liferay Legend 投稿: 1399 参加年月日: 10/09/17 最新の投稿
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
9年前 に Krzysztof Gołębiowski によって更新されました。

RE: Freemarker template - access to themeDisplay

Liferay Master 投稿: 549 参加年月日: 11/06/25 最新の投稿
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
9年前 に James Falkner によって更新されました。

RE: Freemarker template - access to themeDisplay

Liferay Legend 投稿: 1399 参加年月日: 10/09/17 最新の投稿
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
8年前 に Alessandro Lachina によって更新されました。

RE: Freemarker template - access to themeDisplay

New Member 投稿: 24 参加年月日: 13/06/12 最新の投稿
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?
8年前 に Yeray Rodríguez によって更新されました。

RE: Freemarker template - access to themeDisplay

New Member 投稿: 5 参加年月日: 15/03/02 最新の投稿
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-->
8年前 に Traolly Xiong によって更新されました。

RE: Freemarker template - access to themeDisplay

Regular Member 投稿: 195 参加年月日: 11/12/30 最新の投稿
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.
7年前 に Igor Kanshyn によって更新されました。

RE: Freemarker template - access to themeDisplay

New Member 投稿: 4 参加年月日: 12/03/21 最新の投稿
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?
7年前 に Shubhankit Roy によって更新されました。

RE: Freemarker template - access to themeDisplay

New Member 投稿: 1 参加年月日: 14/12/29 最新の投稿
Try using this:


&lt;#assign serviceContext = staticUtil["com.liferay.portal.service.ServiceContextThreadLocal"].getServiceContext()&gt;
&lt;#assign themeDisplay = serviceContext.getThemeDisplay() /&gt;
thumbnail
7年前 に Alessandro Lachina によって更新されました。

RE: Freemarker template - access to themeDisplay

New Member 投稿: 24 参加年月日: 13/06/12 最新の投稿
great! it works
thumbnail
6年前 に Alessandro Candini によって更新されました。

RE: Freemarker template - access to themeDisplay

Regular Member 投稿: 130 参加年月日: 15/10/17 最新の投稿
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
6年前 に Massimo Bevilacqua によって更新されました。

RE: Freemarker template - access to themeDisplay

Regular Member 投稿: 210 参加年月日: 16/12/27 最新の投稿
In liferay 7 the class is:

com.liferay.portal.kernel.service.ServiceContextThreadLocal

and not:

com.liferay.portal.service.ServiceContextThreadLocal
thumbnail
6年前 に Achmed Tyrannus Albab によって更新されました。

RE: Freemarker template - access to themeDisplay

Regular Member 投稿: 158 参加年月日: 10/03/05 最新の投稿
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?