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

Instance Method Summary collapse

Instance Attribute Details

#_childrenObject Also known as: children

The children of this element.



47
48
49
# File 'lib/nitro/element.rb', line 47

def _children
  @_children
end

#_parentObject

The parent of this element.



43
44
45
# File 'lib/nitro/element.rb', line 43

def _parent
  @_parent
end

#_textObject

The text of this element.



52
53
54
# File 'lib/nitro/element.rb', line 52

def _text
  @_text
end

#_viewObject

The view of this element.



56
57
58
# File 'lib/nitro/element.rb', line 56

def _view
  @_view
end

#idObject

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

#closeObject

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:

:hello :world :sidebar



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

#openObject

Prepend this code to the element content.



70
71
# File 'lib/nitro/element.rb', line 70

def open
end

#renderObject

Override this.



125
126
127
# File 'lib/nitro/element.rb', line 125

def render
  "#{open}#{content}#{close}"
end

#render_childrenObject



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