Class: Babylon::Base::View
- Inherits:
-
Object
- Object
- Babylon::Base::View
- Defined in:
- lib/babylon/base/view.rb
Overview
Your application’s views (stanzas) should be descendant of this class.
Instance Attribute Summary collapse
-
#view_template ⇒ Object
readonly
Returns the value of attribute view_template.
Instance Method Summary collapse
-
#evaluate ⇒ Object
“Loads” the view file, and uses the Nokogiri Builder to build the XML stanzas that will be sent.
-
#initialize(path = "", assigns = {}) ⇒ View
constructor
Instantiate a new view with the various varibales passed in assigns and the path of the template to render.
-
#method_missing(sym, *args, &block) ⇒ Object
Used to macth locals variables.
-
#render(xml, options = {}) ⇒ Object
Used to ‘include’ another view inside an existing view.
Constructor Details
#initialize(path = "", assigns = {}) ⇒ View
Instantiate a new view with the various varibales passed in assigns and the path of the template to render.
29 30 31 32 33 34 35 |
# File 'lib/babylon/base/view.rb', line 29 def initialize(path = "", assigns = {}) @view_template = path @locals = {} assigns.each do |key, value| instance_variable_set(:"@#{key}", value) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
Used to macth locals variables
51 52 53 54 |
# File 'lib/babylon/base/view.rb', line 51 def method_missing(sym, *args, &block) raise NameError, "undefined local variable or method `#{sym}' for #{self}" unless @locals[sym] @locals[sym] end |
Instance Attribute Details
#view_template ⇒ Object (readonly)
Returns the value of attribute view_template.
9 10 11 |
# File 'lib/babylon/base/view.rb', line 9 def view_template @view_template end |
Instance Method Details
#evaluate ⇒ Object
“Loads” the view file, and uses the Nokogiri Builder to build the XML stanzas that will be sent.
39 40 41 42 43 44 45 46 47 |
# File 'lib/babylon/base/view.rb', line 39 def evaluate return if @view_template == "" raise ViewFileNotFound, "No such file #{@view_template}" unless Babylon.views[@view_template] builder = Nokogiri::XML::Builder.new builder.stream do |xml| eval(Babylon.views[@view_template], binding, @view_template, 1) end builder.doc.root.children # we output the document built end |
#render(xml, options = {}) ⇒ Object
Used to ‘include’ another view inside an existing view. The caller needs to pass the context in which the partial will be rendered Render must be called with :partial as well (other options will be supported later). The partial vale should be a relative path to another file view, from the calling view. You can also use :locals => => value to use defined locals in your embedded views.
17 18 19 20 21 22 23 24 25 |
# File 'lib/babylon/base/view.rb', line 17 def render(xml, = {}) # First, we need to identify the partial file path, based on the @view_template path. partial_path = (@view_template.split("/")[0..-2] + [:partial].split("/")).join("/").gsub(".xml.builder", "") + ".xml.builder" raise ViewFileNotFound, "No such file #{partial_path}" unless Babylon.views[partial_path] saved_locals = @locals @locals = [:locals] eval(Babylon.views[partial_path], binding, partial_path, 1) @locals = saved_locals # Re-assign the previous locals to be 'clean' end |