Class Skates::Base::View
In: lib/skates/base/view.rb
Parent: Object

Your application‘s views (stanzas) should be descendant of this class.


evaluate   method_missing   new   render  


view_template  [R] 

Public Class methods

Instantiate a new view with the various varibales passed in assigns and the path of the template to render.


    # File lib/skates/base/view.rb, line 29
29:       def initialize(path = "", assigns = {}) 
30:         @view_template = path 
31:         @locals = {}
32:         assigns.each do |key, value| 
33:           instance_variable_set("@#{key}""@#{key}", value) 
34:         end 
35:       end

Public Instance methods

"Loads" the view file, and uses the Nokogiri Builder to build the XML stanzas that will be sent.


    # File lib/skates/base/view.rb, line 39
39:       def evaluate 
40:         return if @view_template == ""
41:         raise ViewFileNotFound, "No such file #{@view_template}" unless Skates.views[@view_template] 
42:         builder = 
43: do |xml|
44:           eval(Skates.views[@view_template], binding, @view_template, 1)
45:         end
46:         builder.doc.root.children # we output the document built 
47:       end

Used to macth locals variables


    # File lib/skates/base/view.rb, line 51
51:       def method_missing(sym, *args, &block)
52:         raise NameError, "undefined local variable or method `#{sym}' for #{self}" unless @locals[sym]
53:         @locals[sym]
54:       end

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 => {:name => value} to use defined locals in your embedded views.


    # File lib/skates/base/view.rb, line 17
17:       def render(xml, options = {})
18:         # First, we need to identify the partial file path, based on the @view_template path.
19:         partial_path = (@view_template.split("/")[0..-2] + options[:partial].split("/")).join("/").gsub(".xml.builder", "") + ".xml.builder"
20:         raise ViewFileNotFound, "No such file #{partial_path}" unless Skates.views[partial_path] 
21:         saved_locals = @locals
22:         @locals = options[:locals]
23:         eval(Skates.views[partial_path], binding, partial_path, 1)
24:         @locals = saved_locals # Re-assign the previous locals to be 'clean'
25:       end