An Invisible Portlet

| | Comments (2)
I had heard of them before, but I had never seen them: Invisible Portlets. Now I can write them.

It turns out there can be a decent use case for an invisible portlet. I wanted to put a stylesheet on a page through a portlet, but I didn't want to have to tweak any of the other portlets to do it. And since the only purpose of my portlet is to deliver a stylesheet, I didn't want the portlet to show up with a header or a title. Perhaps you'll want to use this approach to put some JavaScript on the page for tracking purposes. In any case, the code is pretty simple.

The concept is that when it displays on the page, each portlet object renders with a class ID to let you manipulate it directly. In some cases, you might change the font in the title bar, but in this case, I'm taking the entire region and making it invisible. If you set it invisible through display=none, then it will not be visible to the user, but JavaScript and style instruction still apply to the page.  I use the $$TOKEN$$ concept to access the portet object ID, and that is used in the class ID. Here it is:

<pt:namespace pt:token="$$TOKEN$$" 
xmlns:pt='http://www.plumtree.com/xmlschemas/ptui/'/> <script> alert("Portlet $$TOKEN$$ will magically disappear!"); document.getElementById('pt-portlet-$$TOKEN$$').style.display = 'none'; </script> <link type="text/css"
href="pt://images/plumtree/ptanalytics/private/css/colossus-en.css"
rel="StyleSheet" lang="en" ></link>
For demonstration purposes, that code includes an alert line that will cause the portlet to first notify you that it's really live on the page and then after you acknowledge it, it disappears. You will most likely want to customize the alert message by deleting it :-P

The code is live on this web server, so you can load it into your portal for a look if you'd like. Find it at http://blog.billbenac.com/code/disappearing.portlet.html.

Enjoy!

Update: A Better Approach

Thanks to Steve who pointed out a better way of getting the result I described above. He suggested setting the display to none in CSS directly rather than through JavaScript. That approach would look like this:

<pt:namespace pt:token="$$TOKEN$$" 
xmlns:pt='http://www.plumtree.com/xmlschemas/ptui/'/>

Portlet $$TOKEN$$ should be invisible through CSS.

<style>
#pt-portlet-$$TOKEN$$ { display:none; }
</style>

2 Comments

I've done this before and prefer styles, since an error in javascript might allow it to show. But maybe there is a better reason to use javascript?

<style>
.pt-portlet-$$TOKEN$$ { display:none; }
</style>

Steve, you're absolutely right. It's better to use the style here as opposed to JavaScript.

The reason I went for JavaScript was that the day before I had been working with a related situation in which I had to toggle the display but also toggle whether a string was escaped or not. JavaScript was the answer to that problem, but your recommendation is right when the objective is just to hide the portlet.

I'll update my post.

Thanks,

Bill

Leave a comment