Module: Rubified::Canvas

Defined in:
lib/rubified/canvas.rb

Overview

Used to shorten the creation of tags; just include or extend this module to use it. A small HTML page without Canvas:

Rubified::Tag::Html.new.to_html {
  Rubified::Tag::Head.new.to_html {
    Rubified::Tag::Title.new.to_html {"Generated with Rubified"}
  }
  Rubified::Tag::Body.new.to_html {
    Rubified::Tag::Div.new({:id=>"foo", :class=>"bar"}) {"Hello world!"}
  }
}

Instead you can use:

include Rubifed::Canvas
html {
  head {
    title {"Generated with Rubified"}
  }
  body {
    div({id=>"foo", :class=>"bar"})
  }
}

True, one more line (the include), but a LOT less repetitious and more readable. Both of these will produce:

<html>
  <head>
    <title>Generated with Rubified</title>
  </head>
  <body>
    <div id=>"foo" class=>"bar">Hello world!</div>
  </body>
</html

Class Method Summary collapse

Class Method Details

.add_tag_method(tclass) ⇒ Object

Add a method for a specific XML/HTML tag class.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubified/canvas.rb', line 35

def self.add_tag_method(tclass)
  n = "tag_" + tclass.to_s.split("::").last.downcase
  eval "
    def #{n}(params={}, embedded=false, &block)
      # Determine what class to use by the method name.
      tagclass = #{tclass}
      # Create a new tag then convert it.
      tagclass.new(params).to_html(embedded, &block)
    end
   "
end