Class: Nitro::Element
- Inherits:
-
Object
- Object
- Nitro::Element
- Includes:
- ElementMixin
- Defined in:
- lib/nitro/element.rb,
lib/nitro/element/javascript.rb
Overview
A programmatically generated element.
Usage
in the code
class Page < Nitro::Element
def render
%{
<div id="@id">#{content}</div>
}
end
end
in your template
<Page>hello</Page>
> <div id=“page”>hello</div>
the id is automatically fille with the class name using class.method_name eg. MyModule::MyPage => my_module__my_page
you can override the id to use the element multiple times on the page
Sub Elements
Elements can be imbricated. To render the the child element in the parent’s template, use #:element_id
Design
An underscore is used for the standard attibutes to avoid name clashes. – TODO:
-
separate ‘view’ template files.
++
Direct Known Subclasses
Defined Under Namespace
Classes: Javascript
Instance Attribute Summary
Attributes included from ElementMixin
#_children, #_parent, #_text, #_view, #id
Class Method Summary collapse
-
.compile_template_elements ⇒ Object
Compile the element templates into element classes.
Methods included from ElementMixin
#add_child, #close, #content, #initialize, #open, #render, #render_children
Class Method Details
.compile_template_elements ⇒ Object
Compile the element templates into element classes. Typically called at startup.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/nitro/element.rb', line 221 def compile_template_elements if File.exist? Element.template_root Dir.recurse(Element.template_root) do |filename| if filename =~ /\.#{Nitro::Template.extension}$/ name = File.basename(filename).split('.').first.camelize Nitro::Element.module_eval %{ class #{name} < Nitro::Element def render <<-END_OF_TEMPLATE #{File.read(filename)} END_OF_TEMPLATE end end } end end end end |