Module: Nitro::ElementMixin
- Included in:
- Element
- Defined in:
- lib/nitro/element.rb
Overview
A programmatically generated element. Elements are a form of macros to allow for cleaner templates. They are evaluated at compile time, so there is no performance hit when you use them (at the expense of slightly reduced functionality).
Nitro provides an additional method of defining elements. Instead of creating a lot of small classes, you can put .xhtml templates in the Element template_root. These templates are automatically converted into Element classes.
For extra safety, you are advised to place your classes in the Nitro::Element namespace. If your classes do not extend Nitro::Element, the Nitro::ElementMixin is automatically injected in the class.
An element can have and access a hierarchy of sub-elements. use #:sub_element_name to access the render output of the subelement. Additionaly you can access the whole subelement object: _children
Design
An underscore is used for the standard attibutes to avoid name clashes. – TODO:
-
separate ‘view’ template files.
++
Instance Attribute Summary collapse
-
#_children ⇒ Object
(also: #children)
The children of this element.
-
#_parent ⇒ Object
The parent of this element.
-
#_text ⇒ Object
The text of this element.
-
#_view ⇒ Object
The view of this element.
-
#id ⇒ Object
The id of this element.
Instance Method Summary collapse
- #add_child(child) ⇒ Object
-
#close ⇒ Object
Append this code to the element content.
-
#content(cname = nil) ⇒ Object
If an optional name parameter is passed renders the content of the named child element.
- #initialize(*args) ⇒ Object
-
#open ⇒ Object
Prepend this code to the element content.
-
#render ⇒ Object
Override this.
- #render_children ⇒ Object
Instance Attribute Details
#_children ⇒ Object Also known as: children
The children of this element.
47 48 49 |
# File 'lib/nitro/element.rb', line 47 def _children @_children end |
#_parent ⇒ Object
The parent of this element.
43 44 45 |
# File 'lib/nitro/element.rb', line 43 def _parent @_parent end |
#_text ⇒ Object
The text of this element.
52 53 54 |
# File 'lib/nitro/element.rb', line 52 def _text @_text end |
#_view ⇒ Object
The view of this element.
56 57 58 |
# File 'lib/nitro/element.rb', line 56 def _view @_view end |
#id ⇒ Object
The id of this element.
60 61 62 |
# File 'lib/nitro/element.rb', line 60 def id @id end |
Instance Method Details
#add_child(child) ⇒ Object
138 139 140 141 |
# File 'lib/nitro/element.rb', line 138 def add_child(child) child._parent = self @_children[child.instance_variable_get('@id')] = child end |
#close ⇒ Object
Append this code to the element content.
120 121 |
# File 'lib/nitro/element.rb', line 120 def close end |
#content(cname = nil) ⇒ Object
If an optional name parameter is passed renders the content of the named child element.
eg. #:child_element_id
Example
<Page>
..
<Box id="hello">
..
</Box>
<Box id="world">
..
</Box>
<Sidebar>
..
</Sidebar>
..
</Page>
Access children content from within the enclosing element (Page) like this:
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/nitro/element.rb', line 106 def content(cname = nil) if cname if c = @_children[cname.to_s] c.content else return nil end else @_text end end |
#initialize(*args) ⇒ Object
62 63 64 65 66 |
# File 'lib/nitro/element.rb', line 62 def initialize(*args) @_children = {} @_text = '' @id = self.class.demodulize.underscore end |
#open ⇒ Object
Prepend this code to the element content.
70 71 |
# File 'lib/nitro/element.rb', line 70 def open end |
#render ⇒ Object
Override this.
125 126 127 |
# File 'lib/nitro/element.rb', line 125 def render "#{open}#{content}#{close}" end |
#render_children ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/nitro/element.rb', line 129 def render_children str = '' for c in @_children.values str << c.render end return str end |