Skip to content

Instantly share code, notes, and snippets.

@nuclearsandwich
Created August 31, 2011 21:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuclearsandwich/1184724 to your computer and use it in GitHub Desktop.
Save nuclearsandwich/1184724 to your computer and use it in GitHub Desktop.
Hacktastic HTML DSL
# Only working example so far HTML.new { html { title { "Hello" } } }
# Soon attributes will be working.
class HTML
def initialize &content
@html = ""
instance_eval &content
end
def method_missing tag, *attrs, &content
@html << tag_with_attributes(tag, attrs.first || {})
@html << instance_eval(&content) if content
@html << %|</#{tag}>|
end
def tag_with_attributes tag, attrs=Hash.new
%|<#{tag}| <<
((attrs.empty? and '') or (' ' <<
attrs.map{|attr, val| %|#{attr}="#{val}"|} * ' ')) << '>'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment