Created
July 18, 2012 22:50
-
-
Save jmdfm/3139498 to your computer and use it in GitHub Desktop.
Generate a dynamic CSS class on a body tag with Haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate a dynamic HTML body tag for a passed Haml block. Uses the controller name and | |
# action name to generate a class with a dash between to two, e.g. home-index | |
# | |
# css_class - A CSS class name to use on the body tag. (Optional) | |
# block - The HAML content block with which to surround with a BODY tag. | |
# | |
# Example | |
# | |
# = dynamic_haml_body_tag do | |
# = yield | |
# | |
# Or | |
# | |
# = dynamic_haml_body_tag 'landing-page' do | |
# = yield | |
# | |
# Surrounds the block with a body tag using a generated or specified CSS class name. | |
def dynamic_haml_body_tag(css_class='', &block) | |
# If no css class was specified, then generate one from the current controller and action name. | |
css_class = css_class=='' ? "#{controller.controller_name}-#{controller.action_name}" : css_class | |
# Surround the output of the block with the custom body tag | |
capture_haml do | |
haml_tag 'body', {:class=>css_class} do | |
block.call | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment