Module: Raw::ElementMixin

Included in:
Element
Defined in:
lib/raw/compiler/filter/elements/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 .htmlx 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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_childrenObject Also known as: children

The children of this element.



42
43
44
# File 'lib/raw/compiler/filter/elements/element.rb', line 42

def _children
  @_children
end

#_parentObject

The parent of this element.



38
39
40
# File 'lib/raw/compiler/filter/elements/element.rb', line 38

def _parent
  @_parent
end

#_textObject

The text of this element.



47
48
49
# File 'lib/raw/compiler/filter/elements/element.rb', line 47

def _text
  @_text
end

#_viewObject

The view of this element.



51
52
53
# File 'lib/raw/compiler/filter/elements/element.rb', line 51

def _view
  @_view
end

#idObject

The id of this element.



55
56
57
# File 'lib/raw/compiler/filter/elements/element.rb', line 55

def id
  @id
end

Instance Method Details

#add_child(child) ⇒ Object



153
154
155
156
# File 'lib/raw/compiler/filter/elements/element.rb', line 153

def add_child(child)
  child._parent = self
  @_children[child.instance_variable_get('@id')] = child
end

#closeObject

Append this code to the element content.



135
136
# File 'lib/raw/compiler/filter/elements/element.rb', line 135

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



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/raw/compiler/filter/elements/element.rb', line 101

def content(cname = nil)
  if cname
    if c = @_children[cname.to_s]
      c.content
    else
      return nil
    end
  else
    @_text
  end
end

#include(href, controller = nil) ⇒ Object

Include a text file in the element template. All the conventions of the StaticInclude compiler apply here. Please not that unless you pass an absolute path (starting with ‘/’) you have to pass a controller instance as well.

Example:

def render

%~
<div>
  #{include '/links/latest'}
</div>
~

end



128
129
130
131
# File 'lib/raw/compiler/filter/elements/element.rb', line 128

def include(href, controller = nil)
  filename = StaticIncludeFilter.new.resolve_include_filename(href)
  return File.read(filename)
end

#initialize(*args) ⇒ Object



57
58
59
60
61
# File 'lib/raw/compiler/filter/elements/element.rb', line 57

def initialize(*args)
  @_children = {}
  @_text = ''
  @id = self.class.to_s.demodulize.underscore
end

#openObject

Prepend this code to the element content.



65
66
# File 'lib/raw/compiler/filter/elements/element.rb', line 65

def open
end

#renderObject

Override this.



140
141
142
# File 'lib/raw/compiler/filter/elements/element.rb', line 140

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

#render_childrenObject



144
145
146
147
148
149
150
151
# File 'lib/raw/compiler/filter/elements/element.rb', line 144

def render_children
  str = ''
  for c in @_children.values
    str << c.render
  end
  
  return str
end