代码之家  ›  专栏  ›  技术社区  ›  Shpigford

Haml:有条件的列表项?

  •  0
  • Shpigford  · 技术社区  · 14 年前

    #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 回复  |  直到 14 年前
        1
  •  2
  •   Gav    14 年前

    缩进-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可能是多余的?