Display a Web Content into a Web Content Template

I'm using Liferay 7.0 ga3 and I want to make a carousel (bootstarp) with web content's structure/template(freemarker). The structures allow to display several web content in my carousel. But in my template the cur_WebContent.getData() display the className and the ID of the WebContent :

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

So I use "?keep_after" and "?remove_ending" to get my ID only :

<#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)}

I can display this information in my carrousel like ${web_content_id} but if I use this in my fetchArticle(groupId, articleId), it doesn't work :

    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]
----

Any idea ? Thanks

Upvotes: 1

Views: 2146

Answers (3)

maik mr
maik mr

Reputation: 67

Be sure you have activated the access for serviceLocator variable in freeMarker settings (https://web.liferay.com/en/community/forums/-/message_boards/view_message/73386692#_19_message_74729187) than:

<#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>

I use the Gérôme Hack, and the split instead of simple ?keep_after an ?remove_ending because in a live/staging configuration the variabile is defined in reverse style [mind blasting].

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

Upvotes: 0

Here classPK = "42553" but classPK is different of ID's web content.

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

Upvotes: 1

fustaki
fustaki

Reputation: 1614

I guess web_content_id needs to be transformed to a number

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

Upvotes: 1

Related Questions