« Back

Theming 101: Sub-navigation dropdown blues

June 25, 2010 By Aaron Delani

We've seen this design before, navigation links that somehow bare children that you're able to play with in the comfort of the navigation's "lawn." Now don't get me wrong, there's really no perfect analogy for how simple and efficient dropdowns can be, it's just straightforward fun navigation.

While designing a theme for Liferay, you've probably wondered to yourself, "How easy would it be to actually implement this." Then maybe, you've drifted into the sub-navigation blues... well take it easy, maybe even get your Liferay plugin's sdk out and make some sub-navigation dropdowns with me:

Recently, I've been showing interns how to build drop down navigation items within Liferay themes. I think this information might be useful for those with the "sub-navigation blues."

To start, I developed this on Liferay 5.2 EE SP2, the jQuery javascript should work up to the latest version below 6.0

Building web interface usually involves html, which in our case, we're using Velocity to render html. Velocity is simple templating language developed by Apache. Then we also have the CSS and Javascript to format our sub-navigation visually and functionally.

Our journey starts within the theme:

In my navigation.vm file within the templates folder, Liferay 5.2 includes the for-loop I need in order to get the first child of the current iteration of the navigation item... it starts with a:

#if ($nav_item.hasChildren())

    #foreach ($nav_child in $nav_item.getChildren()) #if ($nav_child.isSelected()) #set ($nav_child_class = "selected") #else #set ($nav_child_class = "") #end
  • $nav_child.getName()
  • #end
#end

 

You can actually inspect the html and find that the child menu is just hidden with plain css. This is controlled through the navigation.css, within the CSS folder. To override this attribute, we'll change the state from within custom.css:

#navigation li:hover .child-menu, .ie6 #navigation li.hover .child-menu { display: block; }

You must be wondering why there's two selectors. To render the hover state in Internet Explorer 6, I needed to use javascript in order to specify the list item's hover state.

Here's some of the code for the actual child menu and the list item that holds it.

#navigation ul li { position: relative } #navigation .child-menu { background: #FFF; border: 1px solid #CCC; border-radius: 3px; -moz-border-radius: 3px; -webkit-border-radius: 3px; box-shadow: 0px 0px 13px #999; -moz-box-shadow: 0px 0px 13px #999; -webkit-box-shadow: 0px 0px 13px #999; left: 0; margin: 0; padding: 3px 0; position: absolute; top: 35px; width: 210px; z-index: 2; }

So in the end, there's just about 378 lines of formatted CSS (5.83kb), and 2 other edited files (navigation.vm 1.52kb and javascript.js 864bytes)... the 4 images came down to 16.3kb for all of them.

Download the theme and check out the code and play around with it. It's not a perfect or production quality theme, but it's a good way to explore how to make sub-navigation dropdowns.

Showing 12 Comments

delang j
6/25/10 9:48 PM

i cant donwload, please help.
thanks.

Bavithra Rajendran
6/28/10 12:49 AM

Thanks for writing about sub-navigation dropdowns emoticon . The link for downloading the theme is not available. Can you please check that out ?

Jonas Yuan
6/28/10 9:28 AM

Theming 101 ... nice. Thank you, Aaron!

Aaron Delani
6/28/10 10:42 AM

Fixed the link... permissions on files in the document library are so robust ;-)

S L B
6/28/10 11:30 AM

FYI, it's easy enough to make subsubmenus to infinity in a similar manner with a recursive Velocity macro. The only issue is that for every change inside the macro, you need to redeploy your theme (or touch web.xml to make Tomcat reload the servlet context) since velocity won't recompile macros by default once they're loaded. But that's only a problem while you're developing.

delang j
6/28/10 7:10 PM

thanks.
very nice.

Senthil Chockalingam
6/29/10 4:54 AM

Hi aaron!,

I have one doubt.

How to remove the revert options in Wiki for End user?.

Revert Option is only for Wiki Admin right?emoticonemoticon

Thanks in Advance..

Aaron Delani
6/29/10 1:32 PM

Hey @SLB, have you tried The Liferay Developer Mode? http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/Liferay+Developer+Mo­de

This way Liferay doesn't use cached the version of the theme you're building?

It'd be a lil crazy to have a fully recursive sub navigation tree... although, I guess that's what site-maps are for, right? :-)

Thomas Berg
6/30/10 12:06 AM

Good article Aaron. I would really like to see an article about theming which targets Liferay 6, covering differences and new possibilities compared to version 5. While still included, jQuery has been replaced by AlloyUI so it would be interesting to see some examples of using AUI in themes.

Aaron Delani
6/30/10 10:09 AM

That's a really great suggestion. There's a whole shift to HTML 5, and Alloy in Liferay 6! I think it'd be great to show how we've really thought about the transition for theme developers. We really care about making feasible improvements, that make sense...

Thomas, you got me going... we'll see. :-)

Luis Rodríguez Fernández
1/17/11 9:18 AM

Hi Aaron,

Really nice article, but the link is broken yet.

Thanks in advance,

Luis

Luis Rodríguez Fernández
1/17/11 9:22 AM

Hi SLB,

I'm also trying to build a submenu using a recursive Velocity Macro. My code is based on the navigation.vm. At the end I check if the selected nav_item has children, if this is true I do a recursive call to the velocity template.

Maybe you could share with us your idea, it sounds really interesting.

Thanks in advance,

Luis