【问题标题】:Display a Web Content into a Web Content Template在 Web 内容模板中显示 Web 内容
【发布时间】:2017-11-29 08:37:36
【问题描述】:

我正在使用 Liferay 7.0 ga3,我想用网页内容的结构/模板(freemarker)制作一个轮播(bootstarp)。 这些结构允许在我的轮播中显示多个 Web 内容。但在我的模板中,cur_WebContent.getData() 显示了 WebContent 的 className 和 ID:

{"className":"com.liferay.journal.model.JournalArticle","classPK":"42553"}

所以我只使用“?keep_after”和“?remove_ending”来获取我的 ID:

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

<#assign web_content_id= cur_WebContent.getData()?keep_after("classPK\":\"")?remove_ending("\"}") >

<#assign cur_articleID = journalArticleLocalService.fetchArticle(groupId, web_content_id)>
${journalArticleLocalService.getArticleContent(cur_articleID, cur_articleID.getDDMTemplateKey(), "VIEW", locale, themeDisplay)}

我可以像 ${web_content_id} 这样在轮播中显示这些信息,但如果我在 fetchArticle(groupId, articleId) 中使用它,它就不起作用:

    FreeMarker template error:
The following has evaluated to null or missing:
==> journalArticleLocalService.fetchArticle(groupId, web_content_id)  [in template "20116#20160#47034" at line 7, column 30]

----
Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----

----
FTL stack trace ("~" means nesting-related):
    - Failed at: #assign cur_articleID = journalArticl...  [in template "20116#20160#47034" at line 7, column 5]
----

有什么想法吗? 谢谢

【问题讨论】:

  • classPK 与 ID 的网页内容不同...

标签: freemarker liferay-7 web-content


【解决方案1】:

我猜web_content_id需要转换成数字

<#assign web_content_id = [...]?number />

【讨论】:

  • 不能是数字。字符串是必需的
【解决方案2】:

这里 classPK = "42553" 但 classPK 与 ID 的网页内容不同。

Astuce : ID = classPK -2 ID = 42553 - 2 = 42551

【讨论】:

    【解决方案3】:

    确保您已在 freeMarker 设置 (https://web.liferay.com/en/community/forums/-/message_boards/view_message/73386692#_19_message_74729187) 中激活对 serviceLocator 变量的访问权限:

    <#assign journalArticleLocalService = serviceLocator.findService("com.liferay.journal.service.JournalArticleLocalService")>
    
    <#assign wcd_obj = webContent.getData() />
    <#list wcd_obj?split(",") as x>
        <#if (x?last_index_of("classPK") != -1)>
            <#assign web_content_id = x?keep_after("classPK\":\"")?remove_ending("\"}")?remove_ending("\"") >
        </#if>
    </#list>
    
    <#if web_content_id??>
        <#assign real_web_content_id = web_content_id?number-2>
        <#assign cur_articleID = journalArticleLocalService.fetchArticle(groupId, real_web_content_id?string)>
        ${journalArticleLocalService.getArticleContent(cur_articleID, cur_articleID.getDDMTemplateKey(), "VIEW", locale, themeDisplay)}
    </#if>
    

    我使用 Gérôme Hack,并使用拆分而不是简单的 ?keep_after 一个 ?remove_ending 因为在实时/暂存配置中定义了变量 以相反的方式[头脑风暴]。

    staging -> {"className":"com.liferay.journal.model.JournalArticle","classPK":"42553"}
    live -> {"classPK":"42553", "className":"com.liferay.journal.model.JournalArticle"}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 2020-04-17
      • 1970-01-01
      • 2021-10-23
      相关资源
      最近更新 更多