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.
-
#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.
25 26 27 28 29 30 31 32 |
# File 'lib/babylon/base/view.rb', line 25 def initialize(path = "", assigns = {}) @view_template = path assigns.each do |key, value| # Babylon.logger.debug("assigning @#{key}='#{value}'") instance_variable_set(:"@#{key}", value) end 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.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/babylon/base/view.rb', line 36 def evaluate return if @view_template == "" # Babylon.logger.debug("eval with template #{@view_template} - looking thru #{Babylon.views}") 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.
16 17 18 19 20 21 |
# File 'lib/babylon/base/view.rb', line 16 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] eval(Babylon.views[partial_path], binding, partial_path, 1) end |