留言板

NavItems from Layouts in Velocity

thumbnail
Chris Becker,修改在11 年前。

NavItems from Layouts in Velocity

Regular Member 帖子: 112 加入日期: 10-6-11 最近的帖子
Hi all;

I have developed a custom navigation.vm file, which displays only the pages in a chosen community, for use in our intranet site. Based on input from this forum, I used Liferay's local service utilities to retrieve Layout objects instead of using the standard $nav_items Velocity variable, using the following code:

#set ($LayoutLocalService = $portal.getClass().forName("com.liferay.portal.service.LayoutLocalServiceUtil").getMethod("getService", null).invoke(null, null))
#set ($GroupLocalService = $serviceLocator.findService("com.liferay.portal.service.GroupLocalService"))
#set($localLayouts = $LayoutLocalService.getLayouts($GroupLocalService.getGroup($company_id, "Lightpath").getGroupId(), false, $getterUtil.getLong("0")))

<nav class="sort-pages" id="navigation">

	<ul>
	#foreach ( $page in $localLayouts )
	    #set ( $page_name = $page.getName($locale) )
           #if(${page_name}!= "Welcome")		
                <li>             
                       <a href="$page.getRegularURL($request)" target="$page.getTarget()"><span>$page_name</span></a>
		</li>
            #end
	#end
	</ul>
</nav>

The problem I am having is that by changing from $nav_items (which are in fact NavItems), I lost the ability to indicate a "selected" state so that I can use css to indicate which navigation item is selected.

Does anyone know how to convert Layouts BACK into NavItems - within a Velocity file - so that I can get at the isSelected() method again?

Regards,

--Chris
thumbnail
David H Nebinger,修改在11 年前。

RE: NavItems from Layouts in Velocity

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
Why not just check out the code for com.liferay.portal.theme.NavItem.isSelected()?

Seems to use the layout and the ThemeDisplay to derive if it's currently enabled, but it looks pretty easy to wrap into your own method...
thumbnail
Chris Becker,修改在11 年前。

RE: NavItems from Layouts in Velocity

Regular Member 帖子: 112 加入日期: 10-6-11 最近的帖子
David H Nebinger:
Why not just check out the code for com.liferay.portal.theme.NavItem.isSelected()?

Seems to use the layout and the ThemeDisplay to derive if it's currently enabled, but it looks pretty easy to wrap into your own method...


HI David;

Thanks for the quick response.

I took a look at the code you suggested, which in turn took me to viewing the com.liferay.portal.model.impl.LayoutImpl.isSelected() method, which looked closer to the task at hand. Here is the code
	public boolean isSelected(
		boolean selectable, Layout layout, long ancestorPlid) {

		if (selectable) {
			long plid = getPlid();

			if ((plid == layout.getPlid()) || (plid == ancestorPlid)) {
				return true;
			}
		}

		return false;
	}


Thinking I could use this in my navigation.vm file (since I was already iterating over Layout objects), I tried the following:
	
    #foreach ( $page in $localLayouts ) #set ( $page_name = $page.getName($locale) ) #if(${page_name}!= "Welcome") #set ($selected = $page.isSelected($selectable, $page, $request.getAncestorPlid())) #if ($selected) <li class="selected"> #else </li><li> </li>


However, this did not work. The part I was not sure about was the getAncestorPlid() method from the $request variable. Any thoughts would be appreciated!

Regards,

--Chris

--Chris
thumbnail
David H Nebinger,修改在11 年前。

RE: NavItems from Layouts in Velocity

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
The RequestVars instance uses the layout's getAncestorPlid() value when it is constructed...
thumbnail
Chris Becker,修改在11 年前。

RE: NavItems from Layouts in Velocity

Regular Member 帖子: 112 加入日期: 10-6-11 最近的帖子
I believe that I was in error with the following line of code:
#set ($selected = $page.isSelected($selectable, $page, $request.getAncestorPlid()))

The $request variable has no getAncestorPlid() method to invoke.

But I am still at a loss as to how to get the selected state to work.

Since, in my example, the $page variable is a layout object that is being iterated over, I am assuming (perhaps incorrectly) that the isSelected method (from LayoutImpl.java) can be invoked on the variable.

And if it can, then it is a matter of passing in the correct parameters to determine if the current page is "selected"

I updated the code to the following:
             #set ($selected = $page.isSelected($selectable, $layout, $layout.getAncestorPlid()))		
     		  #if ($selected)
				  <li class="selected">
			  #else
				  </li>


  • I pass in $layout (found in init.vm) and its ancestor plid method. I also pass in the $selectable variable (also in init.vm), but it is not working.

    Any guidance at all would be appreciated, as I am not any closer to a useful result.

    Regards,

    --Chris
    thumbnail
    Puneet Malode,修改在10 年前。

    RE: NavItems from Layouts in Velocity

    New Member 帖子: 17 加入日期: 12-7-20 最近的帖子
    Hi,
    I am not getting Selected NavItem in template file. Please help me to solve this.
    Please find the below code in my portal-normal.vm file.

    #foreach ($nav_item in $navItems)							
    								#if ($nav_item.hasChildren())
    									#if($nav_item.isChildSelected())								
    										#foreach ($nav_child in $nav_item.getChildren())
    											
    											#if ($nav_child.isSelected())
    												<li class="list-group-item current">											
    											#else
    												</li><li class="list-group-item">
    											#end
    													<a href="$nav_child.getURL()">
    														$nav_child.getName()
    													</a>
    												</li>
    										#end
    									#end																
    								#end
    							#end