views:

154

answers:

2

Hello, i have this Haml view:

!!! strict
 %html{:xmlns => "http://www.w3.org/1999/xhtml"}
   %head
     %meta{:content => "text/html; charset=utf-8", "http-equiv" => "Content-Type"}
     %title
       Lighty | #{@page_title || "Home"}
   %body
     #popup Login here
     #fade
     #wrapper
       #container
         #header
           %a#logo{:href => "/"}
         #menu
           %ul#main
             %li#lighty
               %a{:href => "/"} Lighty
                 %ul
                    %li
                       %a Link here
             %li#community
               %a{:href => "/community"} Community
        #content

And i would like to add an if on line #16 to check if it is the currently visited page and add the class "active" to the li if it returns true. How do i write that if statement there and is there any easier way to do this? My plan now is to use a variable like @current_page and testif it @current_page == "community" and add a class if it returns right. Am i thinking wrong? any easier way to accomplish this?

Thanks for your answer and sorry for my bad english.

+1  A: 

I would highly suggest creating a helper method called visited_class or something similar since it should have access to the current controller/action. Then, you can write all of the code's logic inside of the helper method. This keeps your views drastically cleaner.

You can place this in many places, but since I have so few helpers, I usually just drop it in ApplicationHelper:

def ApplicationHelper

  def visited_class(controller, action)
    if params[:controller] == controller && params[:action] == action
      return " active"
    else
      return ""
    end
  end

end

Then, in your view/HAML file, simply call visited_class and pass in the controller/action that you want to check. You might also be able to use a URL parameter and piece it together from the request.request_uri, but that might be a bit more tedious.

Topher Fangio
Thank you so much!
Micke
Glad to help! If it works out for you, could you please accept my answer so that others know this question is resolved? Thanks and happy coding :-)
Topher Fangio
havent got it to work because i don't know how to put the function in haml, i only get errors :(
Micke
Check out http://haml-lang.com/docs/yardoc/#M000016 about half way down the page. You need to add an equals sign like this `=link_to('Lighty', '/', :class => visited_class(<<your controller here>>, <<your action here>>))` to output ruby code. A dash (-) will run ruby code without outputting it.
Topher Fangio
but i want the class added to the li
Micke
Ah, my apologies. See @KandadaBoggu's answer above. It is a bit more straightforward than my approach (I actually have never worked with HAML, so I was just going based off of the documentation that I briefly read).
Topher Fangio
Thank you for taking the time to answer and don't worry, im happy to get the function :)
Micke
+1  A: 

Add the visited_class method suggested by Topher to the app\helpers\application_helpers.rb file. To add a class to the li element, change your HAML as follows:

%li#lighty{:class => "#{visited_class('pages', 'show')}"}
  %a{:href => "/"} Lighty
    %ul
      %li
        %a Link here
KandadaBoggu
Thank you, now i understand :)
Micke