Class: Doozer::Partial

Inherits:
Object show all
Includes:
Util::Logger, ViewHelpers, ERB::Util
Defined in:
lib/doozer/partial.rb

Overview

This class facilitates loading and rendering of partials.

A partial is an ERB template which starts with an underscore. They behave the same as action view ERB template with the only difference of not having access to Controller instance variables.

An example partial: app/views/controller_name/_partial.html.erb

By default, the Doozer scaffold creates an app/views/global folder which can be used to place global partials like headers, footers, etc.

Partials have access to Doozer::ViewHelpers.

All view helpers in app/helpers are automatically included in the Partial class during app initialize.

A partial can render another partial and so on and so on.

Constant Summary collapse

@@partials =

APP_PATH = Dir.pwd

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ViewHelpers

#alink, #app_name, #app_path, #aurl, #authtoken, #base_url, #feed, #h, #hash_to_props, #hash_to_qs, #img, #ip, #javascript, #link, #metatags, #path, #rack_env, #server_name, #session?, #static_url, #stylesheet, #title, #url

Methods included from Util::Logger

#logger

Constructor Details

#initialize(erb, locals, route) ⇒ Partial

Returns a new instance of Partial.



30
31
32
33
34
35
36
37
38
39
# File 'lib/doozer/partial.rb', line 30

def initialize(erb, locals, route)
  @erb = erb
  @route = route
  if locals.kind_of? Hash
    locals.each_pair {|key, value| 
      #p "#{key}:#{value}"
      self.instance_variable_set("@#{key}".to_sym, value) # :@a, value
    }
  end
end

Instance Attribute Details

#erbObject

Returns the value of attribute erb.



21
22
23
# File 'lib/doozer/partial.rb', line 21

def erb
  @erb
end

#routeObject

Returns the value of attribute route.



21
22
23
# File 'lib/doozer/partial.rb', line 21

def route
  @route
end

Class Method Details

.clear_loaded_partialsObject

Class methods for clearing all cached partials. Mainly a dispatcher for the file watcher to pick up new changes without having to restart the appserver in development mode.



91
92
93
# File 'lib/doozer/partial.rb', line 91

def self.clear_loaded_partials
  @@partials = {}
end

.include_view_helper(helper) ⇒ Object

Class method for including a view helper.



96
97
98
99
# File 'lib/doozer/partial.rb', line 96

def self.include_view_helper(helper)
  m = Doozer::Lib.classify(helper) 
  include Object.const_get(m)
end

.load_partial(name) ⇒ Object

Load and cache partial ERB template with the given file_name.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/doozer/partial.rb', line 78

def self.load_partial(name)
  file = File.join(Doozer::Configs.app_path,"app/views/#{name}.html.erb")
  results = []
  begin
    File.new(file, "r").each { |line| results << line }
    # TODO: throw error if doesn't exist
    @@partials[name] = ERB.new(results.join(""))
  rescue
    puts "ERROR => sorry couldn't load partial #{name} (#{file})"
  end
end

.partial(file = nil, locals = {}, route = route) ⇒ Object

This class method lazily loads and caches the erb templates of the requested partials



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/doozer/partial.rb', line 46

def self.partial(file=nil, locals={}, route=route)
  puts "  Partial: #{file}" 
  
  #p "Class method: Doozer::Partial#partial"
  if file.index("/").nil?
    name = "#{route.controller}/_#{file}" 
  else
    name = "#{file.gsub(/\//,'/_')}"
  end
  load_partial(name) if  @@partials[name].nil?
  erb = @@partials[name]
  if erb
      partial = Doozer::Partial.new(erb, locals, route)
      partial.bind()
  else
    puts "ERROR => no partial exists for #{file}\n"
  end
end

Instance Method Details

#bindObject



41
42
43
# File 'lib/doozer/partial.rb', line 41

def bind
  @erb.result(binding)
end

#partial(file = nil, locals = {}) ⇒ Object

Renders and returns a partial template with the given file_name and local variables.

  • file - expects a string. By default, if you don’t pass a controller, it’s assumed the lookup location is the current route.controller path in the views folder. You must omit the underscore when passing the file_name. A partial is automatically assumed to be html format. It shouldn’t matter if you display an html partial inside a view with a different format.

  • locals - All local key/values are instantiated as instance variables acessable from the partial template. The controller.request variable is appended to locals and is also accessable as an instance variable from the partial template.



72
73
74
75
# File 'lib/doozer/partial.rb', line 72

def partial(file=nil, locals={})
  locals[:request] = @request if not @request.nil?
  Doozer::Partial.partial(file, locals, route=@route)
end