Last active
July 17, 2016 13:38
-
-
Save chrisross/5522091 to your computer and use it in GitHub Desktop.
Custom helper methods to enhance Haml templating in non-Rails projects, ideally combined with guard or Sinatra applications (in this case the partial method would be simplified)
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
require 'haml' unless defined?(Haml) | |
module Haml::Helpers | |
def slugify(str) | |
str.sub(/[^\w]+/,'-').downcase | |
end | |
# Usage in haml document: | |
# = doc_root 6..10, :class => 'no-js', :lang => "en" | |
# %head | |
# %title Document... | |
# | |
# Returns to template: | |
# <!--[if lte IE 6]> <html lang='en' class='no-js ie6'> <![endif]--> | |
# <!--[if IE 7]> <html lang='en' class='no-js ie7'> <![endif]--> | |
# <!--[if IE 8]> <html lang=v 'en' class='no-js ie8'> <![endif]--> | |
# <!--[if IE 9]> <html lang='en' class='no-js ie9'> <![endif]--> | |
# <!--[if (gte IE 10)|!(IE)]> <!--><html lang='en' class='no-js'><!-- <![endif]--> | |
# | |
# ...captured haml here... | |
# | |
# </html> | |
def doc_root(versions, attrs={}, &block) | |
# Convert range to array | |
versions = [*versions].sort.uniq | |
# Uncomment below for default language using html_attrs helper method | |
# attrs.merge!(html_attrs('en-US')) | |
# Capture class if existent classes and flatten incase value is Arrays | |
classes = [(attrs.delete :class if attrs.has_key? :class)].flatten | |
# Collect array of Haml conditional commented html tags | |
template = versions.collect do |ver| | |
format = case ver | |
when versions.first | |
"<!--[if lte IE %d]> <html %s> <![endif]-->" | |
when versions.last | |
"<!--[if (gte IE %d)|!(IE)]><!--> <html %s> <!--<![endif]-->" | |
else | |
"<!--[if IE %d]> <html %s> <![endif]-->" | |
end | |
# Return new Array Object based on classes Array | |
attrs[:class] = Array.new classes | |
attrs[:class] << "ie#{ver}" unless ver == versions.last | |
# Convert Hash key value pairs to a html attribute String | |
attrs_str = attrs.to_a.map do |pair| | |
pair[1] = pair[1].join(' ') if pair[1].is_a?(Array) | |
"%s='%s'" % pair | |
end.join(' ') | |
sprintf format, ver, attrs_str | |
end.join("\n") | |
[ | |
template.chomp, | |
capture_haml(&block).chomp, | |
"</html>" | |
].join("\n") | |
end | |
# Usage in haml document: | |
# | |
# :ruby | |
# locals = {:title => "Page Title"} | |
# | |
# = partial 'layout', locals do | |
# %article | |
# %h1 Page Title | |
# %nav= partial 'navigation' | |
def partial(template, options={}, &block) | |
# expected location of layouts directory is the relative path: | |
# => ../haml/partials/layout_b.haml | |
src_dir = File.expand_path('..', File.dirname(__FILE__)) | |
path = File.join(src_dir,'haml','partials', "#{template}.haml") | |
template = File.read(path) | |
engine = Haml::Engine.new(template) | |
engine.render(Object.new, options) do | |
capture_haml(&block) if block_given? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment