组合视图 统一视图 树状图
讨论主题 [ 上一个 | 下一个 ]
toggle
Chris Becker
NavItems from Layouts in Velocity
2013年2月11日 上午11:44
答复

Chris Becker

等级: Regular Member

帖子: 112

加入日期: 2010年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:

 1#set ($LayoutLocalService = $portal.getClass().forName("com.liferay.portal.service.LayoutLocalServiceUtil").getMethod("getService", null).invoke(null, null))
 2#set ($GroupLocalService = $serviceLocator.findService("com.liferay.portal.service.GroupLocalService"))
 3#set($localLayouts = $LayoutLocalService.getLayouts($GroupLocalService.getGroup($company_id, "Lightpath").getGroupId(), false, $getterUtil.getLong("0")))
 4
 5<nav class="sort-pages" id="navigation">
 6
 7    <ul>
 8    #foreach ( $page in $localLayouts )
 9        #set ( $page_name = $page.getName($locale) )
10           #if(${page_name}!= "Welcome")       
11                <li>            
12                       <a href="$page.getRegularURL($request)" target="$page.getTarget()"><span>$page_name</span></a>
13        </li>
14            #end
15    #end
16    </ul>
17</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
David H Nebinger
RE: NavItems from Layouts in Velocity
2013年2月11日 上午11:52
答复

David H Nebinger

Community Moderator

等级: Liferay Legend

帖子: 11097

加入日期: 2006年9月1日

最近的帖子

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...
Chris Becker
RE: NavItems from Layouts in Velocity
2013年2月11日 下午1:04
答复

Chris Becker

等级: Regular Member

帖子: 112

加入日期: 2010年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
 1    public boolean isSelected(
 2        boolean selectable, Layout layout, long ancestorPlid) {
 3
 4        if (selectable) {
 5            long plid = getPlid();
 6
 7            if ((plid == layout.getPlid()) || (plid == ancestorPlid)) {
 8                return true;
 9            }
10        }
11
12        return false;
13    }


Thinking I could use this in my navigation.vm file (since I was already iterating over Layout objects), I tried the following:
 1    <ul>
 2        #foreach ( $page in $localLayouts )
 3        #set ( $page_name = $page.getName($locale) )
 4           #if(${page_name}!= "Welcome")
 5                     #set ($selected = $page.isSelected($selectable, $page, $request.getAncestorPlid()))       
 6              #if ($selected)
 7                  <li class="selected">
 8              #else
 9                  <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
David H Nebinger
RE: NavItems from Layouts in Velocity
2013年2月11日 下午1:19
答复

David H Nebinger

Community Moderator

等级: Liferay Legend

帖子: 11097

加入日期: 2006年9月1日

最近的帖子

The RequestVars instance uses the layout's getAncestorPlid() value when it is constructed...
Chris Becker
RE: NavItems from Layouts in Velocity
2013年2月12日 下午12:53
答复

Chris Becker

等级: Regular Member

帖子: 112

加入日期: 2010年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:
1             #set ($selected = $page.isSelected($selectable, $layout, $layout.getAncestorPlid()))       
2               #if ($selected)
3                  <li class="selected">
4              #else
5                  <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
Puneet Malode
RE: NavItems from Layouts in Velocity
2014年3月30日 下午11:45
答复

Puneet Malode

等级: New Member

帖子: 17

加入日期: 2012年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.

 1#foreach ($nav_item in $navItems)                           
 2                                #if ($nav_item.hasChildren())
 3                                    #if($nav_item.isChildSelected())                               
 4                                        #foreach ($nav_child in $nav_item.getChildren())
 5                                           
 6                                            #if ($nav_child.isSelected())
 7                                                <li    class="list-group-item current">                                           
 8                                            #else
 9                                                <li    class="list-group-item">
10                                            #end
11                                                    <a href="$nav_child.getURL()">
12                                                        $nav_child.getName()
13                                                    </a>
14                                                </li>
15                                        #end
16                                    #end                                                               
17                                #end
18                            #end