Class: Html
- Includes:
- Enumerable
- Defined in:
- lib/iron/web/html.rb,
lib/iron/web/html/element.rb
Overview
HTML Element Class
Used with the Html class to generate html content for a single tag/element. Represents a single element with attributes and optional contents (including other elements). Generally, you won’t use this by itself. Check out Html.build() instead.
Simple useage:
>> Html::Element.new('span','some text', :id => 'title-text').render
=> '<span id="title-text">some text</span>'
Complex usage:
span = Html::Element.new('span') # Creates a span element for customization
span.id = 'title-text' # Set some attributes
span.style = 'color: #f00;'
span.html = 'some text' # Adds some content
span.render # Converts to html string
=> '<span id="title-text" style="color: #f00;">some text</span>
Defined Under Namespace
Classes: Element
Constant Summary collapse
- HTML_ESCAPE =
Constants
{"&"=>"&", ">"=>">", "<"=>"<", "\""=>"""}.freeze
Class Method Summary collapse
-
.build {|builder| ... } ⇒ Object
Primary entry point for HTML generation using these tools.
-
.escape_once(html) ⇒ Object
Ripped from Rails…
Instance Method Summary collapse
-
#<<(new_item) ⇒ Object
Allow pushing new elements.
- #blank? ⇒ Boolean
-
#comment!(str) ⇒ Object
Inserts an HTML comment (eg <!– yo –>).
- #count ⇒ Object
-
#each ⇒ Object
Implement enumerable.
- #empty? ⇒ Boolean
-
#initialize {|_self| ... } ⇒ Html
constructor
Sets up internal state, natch, and accepts a block that customizes the resulting object.
-
#inspect ⇒ Object
Alias for #render.
- #is_a?(other) ⇒ Boolean
-
#method_missing(method, *args, &block) ⇒ Object
Creates a new element on any method missing calls.
-
#render(depth = 0, inblock = true) ⇒ Object
Renders out as html - accepts depth param to indicate level of indentation.
-
#respond_to_missing?(method, include_private) ⇒ Boolean
Make sure our objects advertise their support of tags.
-
#tag(tag, *args, &block) ⇒ Object
Create a new element explicitly.
-
#text!(str) ⇒ Object
Inserts raw text.
-
#to_s ⇒ Object
Alias for #render.
Constructor Details
#initialize {|_self| ... } ⇒ Html
Sets up internal state, natch, and accepts a block that customizes the resulting object.
45 46 47 48 49 |
# File 'lib/iron/web/html.rb', line 45 def initialize @items = [] @item_stack = [] yield self if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Creates a new element on any method missing calls. Returns self, so you can chain calls (eg html.div(‘foo’).span(‘bar’) )
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/iron/web/html.rb', line 106 def method_missing(method, *args, &block) parts = method.to_s.match(/^([a-z]+[0-9]?)$/) if parts # Assume it's a new element, create the tag tag(parts[1], *args, &block) else # There really is no method... super end end |
Class Method Details
.build {|builder| ... } ⇒ Object
Primary entry point for HTML generation using these tools.
32 33 34 35 36 |
# File 'lib/iron/web/html.rb', line 32 def self.build builder = Html.new yield builder if block_given? builder.render.html_safe end |
.escape_once(html) ⇒ Object
Ripped from Rails…
39 40 41 42 |
# File 'lib/iron/web/html.rb', line 39 def self.escape_once(html) return html if html.html_safe? html.to_s.gsub(/[\"><]|&(?!([a-zA-Z]+|(#\d+));)/) { |special| HTML_ESCAPE[special] }.html_safe end |
Instance Method Details
#<<(new_item) ⇒ Object
Allow pushing new elements
66 67 68 69 70 71 72 73 |
# File 'lib/iron/web/html.rb', line 66 def <<(new_item) if @item_stack.empty? @items << new_item else @item_stack.last.html << new_item end self end |
#blank? ⇒ Boolean
88 89 90 |
# File 'lib/iron/web/html.rb', line 88 def blank? empty? end |
#comment!(str) ⇒ Object
Inserts an HTML comment (eg <!– yo –>)
52 53 54 55 56 57 58 |
# File 'lib/iron/web/html.rb', line 52 def comment!(str) if str.include? "\n" text! "<!--\n#{str}\n-->\n" else text! "<!-- #{str} -->\n" end end |
#count ⇒ Object
80 81 82 |
# File 'lib/iron/web/html.rb', line 80 def count @items.count end |
#each ⇒ Object
Implement enumerable
76 77 78 |
# File 'lib/iron/web/html.rb', line 76 def each @items.each {|v| yield v} if block_given? end |
#empty? ⇒ Boolean
84 85 86 |
# File 'lib/iron/web/html.rb', line 84 def empty? @items.empty? end |
#inspect ⇒ Object
Alias for #render
152 153 154 |
# File 'lib/iron/web/html.rb', line 152 def inspect render end |
#is_a?(other) ⇒ Boolean
156 157 158 |
# File 'lib/iron/web/html.rb', line 156 def is_a?(other) return other == Html end |
#render(depth = 0, inblock = true) ⇒ Object
Renders out as html - accepts depth param to indicate level of indentation
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/iron/web/html.rb', line 128 def render(depth = 0, inblock = true) # Convert elements to strings @items.collect do |item| if item.is_a?(String) if inblock inblock = false ' '*depth + item else item end elsif item.nil? '' else item.render(depth,inblock) end end.join('') end |
#respond_to_missing?(method, include_private) ⇒ Boolean
Make sure our objects advertise their support of tags
118 119 120 121 122 123 124 125 |
# File 'lib/iron/web/html.rb', line 118 def respond_to_missing?(method, include_private) parts = method.to_s.match(/^([a-z]+[0-9]?)$/) if parts true else super end end |
#tag(tag, *args, &block) ⇒ Object
Create a new element explicitly
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/iron/web/html.rb', line 93 def tag(tag, *args, &block) item = Html::Element.new(tag, *args) self << item if block @item_stack.push item block.call(item) @item_stack.pop end return self end |
#text!(str) ⇒ Object
Inserts raw text
61 62 63 |
# File 'lib/iron/web/html.rb', line 61 def text!(str) self << str end |
#to_s ⇒ Object
Alias for #render
147 148 149 |
# File 'lib/iron/web/html.rb', line 147 def to_s render end |