RE: Use attributes without renderRequest in a FreemarkerPortlet

Jamie Sammons, modified 2 Years ago. New Member Posts: 2 Join Date: 7/15/21 Recent Posts

Hello! I am testing a portlet with the views in Freemarker and for now everything is going great, doing some research on the forum I have seen that in order to pass objects to the view we have to use renderRest.setAttribute("test", test) (to send it) and renderRequest.getAttribute("test") (to handle it in the ftl).

is there a way to avoid using the renderRequest.getAttribute and directly put the object(${test}) as in the Templates or as in a Liferay portlet of Spring type?

 


 

Thank you!

thumbnail
David H Nebinger, modified 2 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts

Wow, I didn't think anyone was doing FM portlets anymore...

Liferay uses FM for themes and template handling (for web structures, ADTs, etc) and it uses TemplateContextContributors, but these are really just wrappers that are used when initializing the FM context object to inject values and services into the context so they are available to FM.

I don't know how you're getting FM to handle the rendering of your panel, but at some point you're going to be creating the context object to pass to FM to use to render the template (just like the renderRequest is being injected), so you just have to find out how that context is being created and then the way to inject your values and services into the context.

I could help you with this, but most of the details about how you're building your portlet weren't provided, so this is the best I can do at the moment.

Olaf Kock, modified 2 Years ago. New Member Posts: 2 Join Date: 7/15/21 Recent Posts

Good David, thanks for your answer, we are using a portlet for Liferay 7.3 but we have modified some things:
- We have changed the MVCPortlet to FreeMarkerPortlet.
- We have modified the bnd to integrate the tlds.

In short, we have replicated a little bit the portlet of this Blade example.
https://github.com/liferay/liferay-blade-samples/tree/master/liferay-workspace/apps/freemarker-portlet

I put here a little code to demonstrate how we have the module

Portlet:

public class BladeFreeMarkerPortlet extends FreeMarkerPortlet {
 }

MVCRender:

//@formatter:off
@Component(
        immediate = true, 
        property = { 
                "javax.portlet.name=" + BladeFreeMarkerPortlet.PORTLETID, 
                "mvc.command.name=/",
                "mvc.command.name=/portlet_freemarker/view" 
                }, 
        service = MVCRenderCommand.class
        )
//@formatter:on
public class FreemarkerPortletViewMVCRenderCommand implements MVCRenderCommand {

    @Override
    public String render(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException {
        final ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
        renderRequest.setAttribute("exampleVar", "Emample View Var");
        return "templates/view.ftl";
    }

}

View:

<#include "init.ftl">

<h1>Freemarker Portlet</h1>

<p>
    <b class="redBackground">
        <@liferay_ui["message"] key="example.text.caption" />
    </b>
    ${exampleVar} <-- Error
    
</p>

Would we have to make any modifications to FreeMarkerPortlet?