Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
Alexey Truntov
How to check if layout is selected in vm-template?
December 29, 2011 4:35 AM

Alexey Truntov

Rank: New Member

Posts: 22

Join Date: July 15, 2011

Recent Posts


In our custom theme we iterating through layouts this way:

 2#set ($layoutlocalService=$serviceLocator.findService("com.liferay.portal.service.LayoutLocalService"))
 4#set ($ComPages = $layoutlocalService.getLayouts($orgGroup.getGroupId(),true,0))
 6#foreach ($ComPage in $ComPages)
 8#if ($ComPage.isSelected(true,$ComPage,0))
 9   class="selected"
11    class=""

But this way all layouts marked as selected. Any way to change this code to fix this?

Also, how to determine which layouts will go to $nav_items variable?
Venkat Koppavolu
RE: How to check if layout is selected in vm-template?
December 29, 2011 4:44 AM

Venkat Koppavolu

Rank: Junior Member

Posts: 85

Join Date: July 25, 2010

Recent Posts

Please check with is responsible for setting varibales in velocitycontext pool and can be used in theme.

Line No - 437 onwards in
// Navigation items (should not contain hidden pages and only permissioned to that user)
if (layout != null) {
RequestVars requestVars = new RequestVars(
request, themeDisplay, layout.getAncestorPlid(),

List<NavItem> navItems = NavItem.fromLayouts(
requestVars, layouts);

velocityContext.put("navItems", navItems);

and NavItem is class which has isSelected()

public boolean isSelected() {
ThemeDisplay themeDisplay = _vars.getThemeDisplay();

return _layout.isSelected(
themeDisplay.isTilesSelectable(), themeDisplay.getLayout(),

Hope this will help you.

Olivier ROMAND
RE: How to check if layout is selected in vm-template?
February 20, 2012 2:10 AM

Olivier ROMAND

Rank: Junior Member

Posts: 25

Join Date: January 7, 2012

Recent Posts

Maybe too late but I found a way to customize the pages you want to see displayed:

 2#set($layoutLocalService = $serviceLocator.findService("com.liferay.portal.service.LayoutLocalService"))
 3#set($home_page = $layoutLocalService.getFriendlyURLLayout($group_id, false, "/home"))
 4#set($hierarchy_page = $layoutLocalService.getFriendlyURLLayout($group_id, false, "/plan-de-classement"))
 6<nav class="sort-pages modify-pages" id="navigation">
 7    <ul>
 8        <li>
 9            <a href="$home_page.getFriendlyURL()"><img src="$images_folder/added/p_home_off_white.gif"/></a>
10        </li>
12        #if ($home_page.isSelected(true,$hierarchy_page, $home_page.getAncestorPlid() ))
13            <li class="selected">
14        #else
15            <li>
16        #end
17            <a href="$home_page.getFriendlyURL()" $home_page.getTarget()>$htmlUtil.escape($home_page.getName($locale))</a>
18        </li>
20        #if ($hierarchy_page.isSelected(true,$hierarchy_page, $hierarchy_page.getAncestorPlid() ))
21            <li class="selected">
22        #else
23            <li>
24        #end
25            <a href="$hierarchy_page.getFriendlyURL()" $hierarchy_page.getTarget()>$htmlUtil.escape($hierarchy_page.getName($locale))</a>
26        </li>
27    </ul>

Now, I need to find a way to toogle the selected class correctly (They are all selected as well)
I still struggle on that one, any progress Alexey ?
Jesse Paria
RE: How to check if layout is selected in vm-template?
August 22, 2012 9:44 AM

Jesse Paria

Rank: Junior Member

Posts: 69

Join Date: March 4, 2011

Recent Posts

I had this same problem. I think the problem is in the lack of documentation on this method. It says one of the parameters should be an object of type Layout, however, it doesn't say which layout object. Which I found very confusing. Here is the way that I got it to work:

You need to first get the layout of the current page that is loaded. I did this in init_custom.vm like so
1#set ($currentPage = $themeDisplay.getLayout())

You also have to get all the pages, which I also do in init_custom.vm
1#set ($layouts = $themeDisplay.getLayouts())

Then when looping through each layout in the community, you can call the isSelected() method like so
1#foreach ($layout1 in $layouts)
2#if ($layout1.isSelected(true, $currentPage, $currentPage.getAncestorPlid()))
3#set ($nav_class = "selected")
5#set ($nav_class = "")

If you are building a navigation in this way, which I prefer since it offers you much more flexibility in how the navigation is displayed than just using the $navItems object, you also want to do several other checks, such as making sure the page is not hidden before including it in your navigation.