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.
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.
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?