Portlets Inside Portlets

Portlets are components which create a portal application. They can function without any knowledge of other portlets, or can be configured to be dependent of one another in many ways. You can define events which portlets fire and listen to. You can query the location of another portlet and link to it from your portlet, or even call the service interface the other portlet is offering. One interesting idea is to embed one portlet inside another. Liferay offers a simple tag to do that.

Why would you want to do something like this, you might ask? There are many ways to create robust portlet applications. However, one aspect of good software engineering is the ability to reuse your components without replicating their code. Embedding existing portlets inside your portlet application accomplishes just that, as you don’t have to duplicate any existing code to be able to use it in another portlet.

A recent use-case at Ambientia includes a project where we had created some very basic linking and form sending capability to our application using web content articles to generate forms and including them in layouts where the functionality was needed. At some point we needed to embed the contents of said articles inside a portlet. This was no big deal, as Liferay offers the tools to do just that. However, over time the requirements changed and became more complex, and we noticed that the original solution using web contents did not work as it was supposed to. Whenever we were releasing a new version which changed the way the forms worked, we had to, in addition to updating the applications, remember to update the web contents in every environment we had. And at that point there were a few of them already. It was becoming quite time-consuming, error-prone and tedious job to do.

We decided it was the time to change our approach and export the web content based feature to a custom portlet. At this point the functionality itself was still easily replicated. We had to, however, figure a way to embed this newly created portlet inside another, which previously had embedded the contents of the article defined (it actually applied some dynamic logic to get the article to display). Luckily Liferay offers the liferay-portlet:runtime tag to use in embedding the portlet on custom portlet code.

Embed your portlet using the liferay-portlet:runtime tag

The tag itself is pretty simple, offering you just three parameters to configure the display. These parameters include the fully qualified name of the portlet to include, the query string parameter to pass information to the embedded portlet, and finally default portlet preferences to configure the portlet to include. The parameters offer you some flexibility to embed portlets in different ways depending on some other criteria you might have.

Now that the functionality was inside a portlet, we could dynamically add the portlet in any page we needed. We could also embed it in certain layout templates to make it in every single page using that layout, or include them in a theme to make it appear on every page of the application. And because of the tag library, we could easily use it within our custom portlet as well. And furthermore, we could apply some custom logic to decide if we even should include the portlet or not. In our case it meant checking some properties over the web service we had created which provided the data to the said view.
Portlet included inside another custom portlet
Custom portlet used inside another portlet

All in all it was very straightforward to embed portlets inside another, definitely a technique which can and should be considered as a viable option if something similar is needed. I’m interested to hear if someone else has used this functionality, what was your use-case?