Raul Maiz
Raul Maiz

Reputation: 13

How to display plain HTML of Web Content with Freemarker in Liferay

In Liferay Portal 7.2, I have list of elements that are Web Contents. For each Web Content in this list I want to get the HTML result defined in the web content itself.

Having an ADT restricted to one structure with a default template, to list every Web Content it's working for me:

<#list entries as entry>                    
        
    <#if hasCategoryId(entry, selectedYearCategoryId) &&
            hasCategoryId(entry, selectedTipoNormativaCategoryId)>
            

        <#assign assetRenderer = entry.getAssetRenderer()/>
        <#assign journalArticle = assetRenderer.getAssetObject()/>

            <@liferay_journal["journal-article"]
                articleId=journalArticle.getArticleId()
                groupId=journalArticle.getGroupId()/>                                    

    </#if>

</#list>

"hasCategoryId" is a custom function and this ADT embedded needs to reload the entire page passing the parameteres to itself in the URL.

I was wondering if I want to do it using Ajax and do this stuff in a MVCRenderCommand MVCResourceCommand, exists any way to convert a Web Content into HTML giving structureId, articleId, ...

Something like:

JournalArticle.getHTML(structureId, templateId);

Upvotes: 1

Views: 1679

Answers (3)

Pedro Blandim
Pedro Blandim

Reputation: 380

I think you are looking for this method

    _journalArticleLocalService.getArticleDisplay(
                    themeDisplay.getScopeGroupId(), articleId, 
                    templateKey, null, themeDisplay.getLanguageId(), 
                    themeDisplay).getContent();

you can see more about it on the docs.

As you want to call this code with an Ajax call, you should probably be using MVCResourceCommand. The code would be something like this:

package com.your.packace;

import com.liferay.journal.service.JournalArticleLocalService;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCResourceCommand;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCResourceCommand;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

@Component(
        immediate = true,
        property = {
                "javax.portlet.name=" + YourPortletKeys.YOUR_PORTLET_NAME,
                "mvc.command.name=/your/command/name"
        },
        service = MVCResourceCommand.class
)
public class GetArticleDisplayMVCResourceCommand extends BaseMVCResourceCommand {

    @Override
    protected void doServeResource(
            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
        throws Exception {

        ThemeDisplay themeDisplay =
                (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);

        String articleId = ParamUtil.getString(resourceRequest, "articleId");
        String templateKey = ParamUtil.getString(resourceRequest, "templateKey");

        JSONObject response = JSONFactoryUtil.createJSONObject();

        String html = _journalArticleLocalService.getArticleDisplay(
                themeDisplay.getScopeGroupId(), articleId,
                templateKey, null, themeDisplay.getLanguageId(),
                themeDisplay).getContent();

        response.put("html", html);

        resourceResponse.setContentType(ContentTypes.APPLICATION_JSON);
        resourceResponse.getWriter().write(response.toString());
    }

    @Reference
    private JournalArticleLocalService _journalArticleLocalService;
}

I did not test this code, so let me know if somethig is wrong.

Upvotes: 1

ferdez
ferdez

Reputation: 11

Sorry, I got a bit confused by the references to Ajax and MVCRenderCommand...

Rereading your question I suggest that you simple add a templateKey to your tag invocation, as per https://docs.liferay.com/dxp/apps/web-experience/7.0.11/taglibdocs/liferay-journal/journal-article.html

Something like:

<@liferay_journal["journal-article"]
            articleId=journalArticle.getArticleId()
            ddmTemplateKey="654321"
            groupId=journalArticle.getGroupId()/> 

Upvotes: 0

ferdez
ferdez

Reputation: 11

If you're going into Ajax, use the Headless API. You can easily fetch content as JSON or HTML.

Upvotes: 0

Related Questions