Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

refactor conditions within haml view

beside the fact that accessibility standards discourage the use of a link pointing to the current page, how I am supposed to refactor the following view code?

#navigation
  %ul.tabbed
    - if current_page?(new_profile_path)
      %li{:class => "current_page_item"}
        = link_to t("new_profile"), new_profile_path
    - else
      %li
        = link_to t("new_profile"), new_profile_path

    - if current_page?(profiles_path)
      %li{:class => "current_page_item"}
        = link_to t("profiles"), profiles_path
    - else
      %li
        = link_to t("profiles"), profiles_path
    ...

Thank you.

like image 754
user167267 Avatar asked Sep 02 '09 13:09

user167267


2 Answers

# helpers
def current_page_class(page)
  return :class => "current_page_item" if current_page?(page)
  return {}
end

-# Haml
#navigation
  %ul.tabbed
    %li{current_page_class(new_profile_path)}
      = link_to t("new_profile"), new_profile_path
    %li{current_page_class(profiles_path)}
      = link_to t("profiles"), profiles_path
    ...
like image 126
Natalie Weizenbaum Avatar answered Oct 14 '22 03:10

Natalie Weizenbaum


#navigation
  %ul.tabbed
    %li{:class => current_page?(new_profile_path) ? "current_page_item" :nil }
      = link_to t("new_profile"), new_profile_path
    %li{:class => current_page?(profiles_path) ? "current_page_item" :nil }
      = link_to t("profiles"), profiles_path
    ...
like image 35
BvuRVKyUVlViVIc7 Avatar answered Oct 14 '22 02:10

BvuRVKyUVlViVIc7