Search code examples
liferayvelocityliferay-theme

how to create dynamic items in liferay theme as per the values in the portlet


I have a theme in liferay where there are some items in the side menu, I want to add some more items to the menu dynamically based on the values i obtained in the portlet's controller.

My theme is like this :

<div id="menu">
    <ul class="link" style="height: 609px;">    
        <li><a href="$themeDisplay.getPortalURL()/x" id="x" class="active">My Account<i class="pull-right" ></i></a></li>
        <li><a href="$themeDisplay.getPortalURL()/y" id="y" class="active">Settings<i class="pull-right" ></i></a></li>
    </ul>
</div>

I have a portlet from where I gets some values in a list

List<String> list= new ArrayList<String>();
list.add("test1");
renderRequest.setAttribute("list", list);

The list could have different values.

What I want is if I have a parameter called test1 in the list , I want to add a new parameter in the theme to be available for that particular user.

<li><a href="$themeDisplay.getPortalURL()/z" id="z" class="active">Bonus<i class="pull-right" ></i></a></li>

If it was JSP, I would have used the but how can it be done in the liferay theme.(I am using velocity theme).


Solution

  • As per Olaf's reply,you should probably proceed with a better approach to this design.You can however use attributes from Servlet context in theme. In your portlet class:

        HttpServletRequest request=PortalUtil.getHttpServletRequest(req);
        ServletContext servletContext=request.getSession().getServletContext();
        servletContext.setAttribute("param", "Product3");
    

    which can then be retrieved in theme via request variable available in theme velocity template:

        $request.getSession().getServletContext().getAttribute("param")