Skip to content

Instantly share code, notes, and snippets.

@jmdfm
Created July 18, 2012 22:50
Show Gist options
  • Save jmdfm/3139498 to your computer and use it in GitHub Desktop.
Save jmdfm/3139498 to your computer and use it in GitHub Desktop.
Generate a dynamic CSS class on a body tag with Haml
# 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