Haml:有条件的列表项目?

时间:2010-11-01 14:38:11

标签: ruby-on-rails 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)

问题是if条件是最后一个列表项...这样做会呈现一个关闭的</ul>标记,然后是另一个列表项。我需要的是,如果最后一个列表项满足条件,那么该列表项将成为无序列表的一部分。

我该怎么做?

1 个答案:

答案 0 :(得分: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,如果他们可以访问个人资料和帐户链接,那么if @user可能是多余的吗?

相关问题