Class: Mime::Engine
- Inherits:
-
Object
- Object
- Mime::Engine
- Defined in:
- lib/mime-components/engine/engine.rb,
lib/mime-components/engine/render.rb,
lib/mime-components/engine/globals.rb,
lib/mime-components/extensions/erb.rb,
lib/mime-components/components/components.rb
Overview
The Engine is the primary class responsible for rendering Mime documents. Each engine contains a component registry as well as a set of built-in components.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Get a global template variable.
-
#[]=(name, value) ⇒ Object
Set a global template variable.
-
#erb(template, vars) ⇒ String
Render an ERB template with Erubi, then post-process with this engine.
-
#initialize ⇒ Engine
constructor
Initialize a new instance of the Mime templating engine.
-
#keys ⇒ Array[Object]
Get a list of set global variables.
-
#register_component(component) ⇒ Object
Register a new component for this engine.
-
#render(template, vars) ⇒ Object
Render all components in a ‘template’.
-
#render_component(name, xml, vars) ⇒ Object
Render a component based on its parsed XML tag.
Constructor Details
#initialize ⇒ Engine
Initialize a new instance of the Mime templating engine.
10 11 12 13 14 15 |
# File 'lib/mime-components/engine/engine.rb', line 10 def initialize @global_vars = {} @registered_components = {} @component_regex = %r{(<[MmCc]:((?:[A-z0-9\:\-]+)|\#).*?/?>)} Mime.register_defaults(self) end |
Instance Method Details
#[](name) ⇒ Object
Get a global template variable.
20 21 22 |
# File 'lib/mime-components/engine/globals.rb', line 20 def [](name) @global_vars[name] end |
#[]=(name, value) ⇒ Object
Set a global template variable.
13 14 15 |
# File 'lib/mime-components/engine/globals.rb', line 13 def []=(name, value) @global_vars[name] = value end |
#erb(template, vars) ⇒ String
Render an ERB template with Erubi, then post-process with this engine.
37 38 39 40 |
# File 'lib/mime-components/extensions/erb.rb', line 37 def erb(template, vars) template_output = instance_eval(template.src) render(template_output, vars) end |
#keys ⇒ Array[Object]
Get a list of set global variables.
26 27 28 |
# File 'lib/mime-components/engine/globals.rb', line 26 def keys @global_vars.keys end |
#register_component(component) ⇒ Object
Register a new component for this engine.
42 43 44 |
# File 'lib/mime-components/components/components.rb', line 42 def register_component(component) @registered_components[component.name.downcase] = component end |
#render(template, vars) ⇒ Object
Render all components in a ‘template’.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mime-components/engine/render.rb', line 10 def render(template, vars) @global_vars.each_key do |k| vars[k] = @global_vars[k] end # we locate, parse and replace tags to render the template. none of these # things work with frozen literals, so we'll make a copy to work on and return output = template.dup # find tags matches = output.scan(@component_regex) matches.each do |n| # render tag tag, component_name = n tag_out = if component_name == '#' '' else render_component(component_name, tag, vars) end # replace tag in output with its rendered value output.gsub!(tag, tag_out) end # freeze and return output output.freeze output end |
#render_component(name, xml, vars) ⇒ Object
Render a component based on its parsed XML tag.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mime-components/components/components.rb', line 50 def render_component(name, xml, vars) # get component, raising an error if it doesn't exist if (component = @registered_components[name.downcase]).nil? raise "#{xml}: component '#{name}' was not registered for this engine" end # normalize attributes xml = Nokogiri::XML.parse(xml).root attributes = {} xml.attributes.map do |k, v| attributes[k.to_s.strip.downcase.to_sym] = v end # render subcomponents of output render(component.render(attributes, vars).to_s, vars) end |