Class: Ezamar::Element
- Includes:
- Ramaze::Helper::Methods
- Defined in:
- lib/ramaze/template/ezamar/element.rb
Overview
an Element is almost like an Controller, however, instead of connecting actions to templates it is only used in Ramaze::Template::Ezamar and can be used inside the templates of the Controller as a simple wrapper.
Example:
Your Element called Page:
class Page < Ezamar::Element
def render
%{
<html>
<h1>
#@title
</h1>
#{content}
</html>
}
end
end
and one called SideBar
class SideBar < Ezamar::Element
def render
%{
<a href="http://something.com">something</a>
}
end
end
and your template (any template for any action):
<Page title="Test">
<SideBar />
<p>
Hello, World!
</p>
</Page>
would result in:
<html>
<h1>
Test
</h1>
<p>
Hello, World!
</p>
</html>
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
Class Method Summary collapse
-
.demunge_passed_variables(template) ⇒ Object
basically processes stuff like ‘foo=“bar” foobar=“baz”’ do NOT pass actual objects that cannot be simply read as a string here, the information will be lost.
-
.finish_transform(klass, params, content) ⇒ Object
find the element, create an instance, pass it the content check if it responds to :render and sets instance-variables that are named after the keys and hold the values of the parameters you passed to the Element.
-
.transform(template) ⇒ Object
transforms all <Element> tags within the string, takes also a binding to be compatible to the transform-pipeline, won’t have any use for it though.
-
.transform_with_content(template, klass) ⇒ Object
transforms elements like: <Page> some content </Page>.
-
.transform_without_content(template, klass) ⇒ Object
transforms elements like: <Page />.
Instance Method Summary collapse
-
#initialize(content) ⇒ Element
constructor
this will be called by #transform, passes along the stuff inside the tags for the element.
-
#render(*args) ⇒ Object
The method that will be called upon to render the things inside the element, you can access #content from here, which contains the contents between the tags.
Methods included from Ramaze::Helper::Methods
extend_object, #helper, included
Constructor Details
#initialize(content) ⇒ Element
this will be called by #transform, passes along the stuff inside the tags for the element
66 67 68 |
# File 'lib/ramaze/template/ezamar/element.rb', line 66 def initialize(content) @content = content end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
57 58 59 |
# File 'lib/ramaze/template/ezamar/element.rb', line 57 def content @content end |
Class Method Details
.demunge_passed_variables(template) ⇒ Object
basically processes stuff like
'foo="bar" foobar="baz"'
do NOT pass actual objects that cannot be simply read as a string here, the information will be lost.
Exceptions are true, false, Integers and Floats. They will appear in their real form (this again is also valid for strings that contain these values in a way that makes Integer/Float possible to parse them)
Just remember, walk like a duck, talk like a duck.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/ramaze/template/ezamar/element.rb', line 154 def demunge_passed_variables(template) template.scan(/\s?(.*?)=["'](.*?)["']/).inject({}) do |hash, (key, value)| value = case value when 'true' true when 'false' false else Integer(value) rescue Float(value) rescue value end hash.merge key => value end end |
.finish_transform(klass, params, content) ⇒ Object
find the element, create an instance, pass it the content check if it responds to :render and sets instance-variables that are named after the keys and hold the values of the parameters you passed to the Element
Parameters look like:
<Page foo="true"> bar </Page>
<Page foo="true" />
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/ramaze/template/ezamar/element.rb', line 124 def finish_transform(klass, params, content) scope = if Ramaze::Action.current Ramaze::Action.current.controller else self.class end instance = scope.constant(klass).new(content) demunge_passed_variables(params).each do |key, value| instance.instance_variable_set("@#{key}", value) end instance.render rescue => ex Ramaze::Log.debug(ex.) '' end |
.transform(template) ⇒ Object
transforms all <Element> tags within the string, takes also a binding to be compatible to the transform-pipeline, won’t have any use for it though.
85 86 87 88 89 90 91 92 93 |
# File 'lib/ramaze/template/ezamar/element.rb', line 85 def transform template matches = template.scan(/<([A-Z][a-zA-Z0-9]*)(.*?)?>/) matches.each do |(klass, params)| transformer = (params[-1,1] == '/' ? :without : :with) template = send("transform_#{transformer}_content", template, klass) end template end |
.transform_with_content(template, klass) ⇒ Object
transforms elements like:
<Page> some content </Page>
98 99 100 101 102 103 |
# File 'lib/ramaze/template/ezamar/element.rb', line 98 def transform_with_content(template, klass) template.gsub(/<#{klass}( .*?)?>(.*?)<\/#{klass}>/m) do |m| params, content = $1.to_s, $2.to_s finish_transform(klass, params, content) end end |
.transform_without_content(template, klass) ⇒ Object
transforms elements like:
<Page />
108 109 110 111 112 113 |
# File 'lib/ramaze/template/ezamar/element.rb', line 108 def transform_without_content(template, klass) template.gsub(/<#{klass}( .*?)?\/>/) do |m| params = $1.to_s finish_transform(klass, params, content = '') end end |
Instance Method Details
#render(*args) ⇒ Object
The method that will be called upon to render the things inside the element, you can access #content from here, which contains the contents between the tags.
It should answer with a String.
76 77 78 |
# File 'lib/ramaze/template/ezamar/element.rb', line 76 def render *args @content end |