2010-11-01 38 views
0

我有以下塊:Haml:有條件的列表項?

#control-panel 
    %h3 
    = "Manage" 
    %ul.left-panel 
    %li{:class => 'my-profile'} 
     = link_to 'Profile', edit_user_path(current_user) 
    %li{:class => 'my-account'} 
     = link_to 'Account', edit_account_user_path(current_user) 
    -if @user && current_user.parent? 
    %li{:class => 'my-blog'} 
     = link_to 'Blog', manage_user_posts_path(current_user) 

的問題是條件上最後一個列表項目,如果...做這樣的渲染封閉</ul>標籤,然後另一個列表項。我需要的是,如果最後的列表項符合條件,那麼它將成爲無序列表的一部分。

我該怎麼做?

回答

2

縮進-if和隨後的%li,因此它們與上面的%li元素處於同一列。

%ul.left-panel 
    %li{:class => 'my-profile'} 
    = link_to 'Profile', edit_user_path(current_user) 
    %li{:class => 'my-account'} 
    = link_to 'Account', edit_account_user_path(current_user) 
    -if @user && current_user.parent? 
    %li{:class => 'my-blog'} 
     = link_to 'Blog', manage_user_posts_path(current_user) 

而且,只是看你的代碼,我猜訪問者已經是@user,如果他們可以訪問個人資料和賬戶鏈接,所以如果@user可能是多餘的?