Reputation: 417
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).
Upvotes: 2
Views: 393
Reputation: 805
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")
Upvotes: 2
Reputation: 48057
There's quite some discussion on this already in the comments to your question. Your theme can't access "your portlet's controller", it's the wrong way to think about the problem: If a portlet determines what to show, you should embed the portlet in the theme. After all, you probably want to interact with it, and that's the easiest way to do so.
Even if a portlet is present on a page, a theme can't arbitrarily use any portlet's request attributes - they're well shielded from accessing each other by design: Otherwise you'd have all kinds of conflicts between rogue portlets and themes.
And as long as you can't guarantee that a portlet is on the page, you can't just call arbitrary methods on the portlet anyways. (not that you could otherwise).
If you want to go through with implementing this functionality in a theme, say goodbye to "the portlet controller". You should have your code in an independent utility class, as a portlet in this case is just the wrong place for the implementation.
I'm not fully understanding your complete usecase, so I can't tell you which implementation makes most sense. Only that the combination you outline in your question makes the least sense to me.
Upvotes: 3