留言板

Liferay 6.2 CMS Templates - Query content item (Freemarker)

Traolly Xiong,修改在8 年前。

Liferay 6.2 CMS Templates - Query content item (Freemarker)

Regular Member 帖子: 195 加入日期: 11-12-30 最近的帖子
Hello Community,
We've implemented code in a theme (freemarker) to query a content item and it works as expected.
Using the same approach within the CMS templates (freemarker), it errors out and fails the attempt.

Code Snippet:

<#assign primWidget = primWidget.getData() />
<#assign articleId = "4762312" />
<#assign temp1 = primWidget?replace(" ", "-") />
<#assign temp2 = temp1?replace("--", "-") />
<#assign temp3 = temp2?lower_case />
<#assign webContentURLTitle = temp3>

<#assign groupid = themeDisplay.getScopeGroupId()>
<#assign journalService = serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService") />

<#attempt>
<#if journalService??>
<#attempt>
<#--assign journalArticle = journalService.getLatestArticleByUrlTitle(group_id, webContentURLTitle, 0) /-->
<#--assign journalArticle = journalService..getArticleByUrlTitle(group_id, webContentURLTitle) /-->
<#assign journalArticle = journalService.getArticle(group_id, articleId) />
<#recover>
Attempting to get journal article failed!
</#recover>
<#if (journalArticle??)>
${journalContentUtil.getContent(group_id?long,"${journalArticle.getArticleId()}","", "${locale}", themeDisplay)}
</#if>
</#if>
<#recover>
Web Content Not Found.
</#recover>

Any thoughts on why this is happening?

Thanks.

Traolly Xiong
thumbnail
Pete Helgren,修改在8 年前。

RE: Liferay 6.2 CMS Templates - Query content item (Freemarker)

Regular Member 帖子: 225 加入日期: 11-4-7 最近的帖子
What is the error? Java error? Freemarker template error?
thumbnail
James Falkner,修改在8 年前。

RE: Liferay 6.2 CMS Templates - Query content item (Freemarker)

Liferay Legend 帖子: 1399 加入日期: 10-9-17 最近的帖子
When you move to Web Content Templates, you lose some of the functionality that Themes get.

Traolly Xiong:
Hello Community,
We've implemented code in a theme (freemarker) to query a content item and it works as expected.
Using the same approach within the CMS templates (freemarker), it errors out and fails the attempt.

Code Snippet:

<#assign primWidget = primWidget.getData() />
<#assign articleId = "4762312" />
<#assign temp1 = primWidget?replace(" ", "-") />
<#assign temp2 = temp1?replace("--", "-") />
<#assign temp3 = temp2?lower_case />
<#assign webContentURLTitle = temp3>


This can be shortened by chaining the builtins. So replace the above with

&lt;#assign webContentURLTitle = primWidget.data?replace(" ", "-")?replace("--", "-")?lower_case&gt;


<#assign groupid = themeDisplay.getScopeGroupId()>


This is not going to work in CMS templates, because themeDisplay isn't a "real" instance of ThemeDisplay. It's just a map. So use

&lt;#assign group_id = getterUtil.getLong(request['theme-display']['scope-group-id']) /&gt;


Also did you mean to use group_id or groupid? I think you meant group_id.


<#assign journalService = serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService") />

<#attempt>
<#if journalService??>
<#attempt>
<#--assign journalArticle = journalService.getLatestArticleByUrlTitle(group_id, webContentURLTitle, 0) /-->
<#--assign journalArticle = journalService..getArticleByUrlTitle(group_id, webContentURLTitle) /-->
<#assign journalArticle = journalService.getArticle(group_id, articleId) />
<#recover>
Attempting to get journal article failed!
</#recover>
<#if (journalArticle??)>
${journalContentUtil.getContent(group_id?long,"${journalArticle.getArticleId()}","", "${locale}", themeDisplay)}
</#if>
</#if>
<#recover>
Web Content Not Found.
</#recover>


As stated above, themeDisplay is not a "real" ThemeDisplay object so you can't pass it to the journalContentUtil.getContent.

You can create a "fake" ThemeDisplay object but I don't think it's going to work (I've tried to render journal articles inline inside of freemarker WCM templates before, and have had no success). But you can create a 'fake' ThemeDisplay using:

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

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

And then try to pass that (but I still think it's going to fail, others have tried though).
thumbnail
Julien Mourad,修改在8 年前。

RE: Liferay 6.2 CMS Templates - Query content item (Freemarker)

Junior Member 帖子: 78 加入日期: 15-1-22 最近的帖子
James Falkner:


<#assign groupid = themeDisplay.getScopeGroupId()>


This is not going to work in CMS templates, because themeDisplay isn't a "real" instance of ThemeDisplay. It's just a map. So use

&lt;#assign group_id = getterUtil.getLong(request['theme-display']['scope-group-id']) /&gt;


You could simply use the @group_id@ token. (not quite sure if groupId and scopeGroupId are the same or not)

Something I believe is worth mentioning, at runtime, several tokens, included in Journal elements (Article, Structure, Template), will be translated to their applicable runtime value at processing time. Here is a complete list of tokens and their runtime values:

@cdn_host@ : themeDisplay.getCDNHost()
@company_id@ : themeDisplay.getCompanyId()
@group_id@ : groupId
@cms_url@ : themeDisplay.getPathContext() + "/cms/servlet"
@image_path@ : themeDisplay.getPathImage()
@friendly_url_private_group@ : themeDisplay.getPathFriendlyURLPrivateGroup()
@friendly_url_private_user@ : themeDisplay.getPathFriendlyURLPrivateUser()
@friendly_url_public@ : themeDisplay.getPathFriendlyURLPublic()
@main_path@ : themeDisplay.getPathMain()
@portal_ctx@ : themeDisplay.getPathContext()
@portal_url@ : Http.removeProtocol(themeDisplay.getURLPortal())
@root_path@ : themeDisplay.getPathContext()
@theme_image_path@ : themeDisplay.getPathThemeImages()
@language_id@ : the language_id of the current request
Traolly Xiong,修改在8 年前。

RE: Liferay 6.2 CMS Templates - Query content item (Freemarker)

Regular Member 帖子: 195 加入日期: 11-12-30 最近的帖子
Thanks for the list of the runtime values!
Traolly Xiong,修改在8 年前。

RE: Liferay 6.2 CMS Templates - Query content item (Freemarker)

Regular Member 帖子: 195 加入日期: 11-12-30 最近的帖子
Hello James,

After doing some more testings, the creation of the dummy theme object allowed me to query a web content within the CMS templates (freemarker).
Very cool!

It would seem there are limited info online in regards to Liferay freemarker coding, so having developers like yourself helping to
guide and answer questions on this forum is very much appreciated.

Thanks!

Traolly Xiong

One other thing, I did notice that this works on my local machine just fine, but on our DEV server, this error occurs:

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

Any thoughts?
Traolly Xiong,修改在8 年前。

RE: Liferay 6.2 CMS Templates - Query content item (Freemarker)

Regular Member 帖子: 195 加入日期: 11-12-30 最近的帖子
After some more debugging, I think a restart of the server resolved the issue.

NOTE: It's also best to use the web content title and NOT the articleId because the articleId is different on the staging and live site.

Much thanks.