Class: Hoshi::Tag
- Inherits:
-
Object
- Object
- Hoshi::Tag
- Defined in:
- lib/hoshi/tag.rb
Overview
Represents an HTML tag. You usually won’t be using this class directly.
Instance Attribute Summary collapse
-
#close_type ⇒ Object
Returns the value of attribute close_type.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name, close_type = nil) ⇒ Tag
constructor
A tag currently has only two attributes: a name and a method for closing it, which both decide how it is rendered as a string.
-
#render(inside = nil, opts = {}) ⇒ Object
Generates a string from this tag.
Constructor Details
#initialize(name, close_type = nil) ⇒ Tag
A tag currently has only two attributes: a name and a method for closing it, which both decide how it is rendered as a string. A self-closing tag:
Tag.new('test', :self).render # => "<test />"
A tag that does not need to close:
Tag.new('test', :none).render('test this') # => "<test>test this\n"
And a regular tag:
Tag.new('test').render # => "<test></test>"
14 15 16 |
# File 'lib/hoshi/tag.rb', line 14 def initialize(name, close_type = nil) @name, @close_type = name, close_type end |
Instance Attribute Details
#close_type ⇒ Object
Returns the value of attribute close_type.
4 5 6 |
# File 'lib/hoshi/tag.rb', line 4 def close_type @close_type end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/hoshi/tag.rb', line 4 def name @name end |
Instance Method Details
#render(inside = nil, opts = {}) ⇒ Object
Generates a string from this tag. inside should be the contents to put between the opening and closing tags (if any), and opts are the HTML options to put in the tag. For example,
Tag.new('div').render('Click for an alert.',
:onclick => "alert('Hi.');")
gets you this:
<div onclick="alert('Hi.');">Click for an alert.</div>
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/hoshi/tag.rb', line 25 def render(inside = nil, opts = {}) inside = inside s = "<#{name} #{opts.}" s.chomp! ' ' if((!inside || inside.empty?) && close_type == :self) return s << " />" end s << ">" s << inside if inside if close_type == :none s << "\n" else s << "</#{name}>" end end |