Class: Mack::Rendering::ViewTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/mack/rendering/view_template.rb

Overview

This class is used to do all the view level bindings. It allows for seperation between the Mack::Controller and the view levels.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(render_type, render_value, options = {}) ⇒ ViewTemplate

Returns a new instance of ViewTemplate.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mack/rendering/view_template.rb', line 12

def initialize(render_type, render_value, options = {})
  self._render_type = render_type
  self._render_value = render_value
  self._options = options
  @_yield_to_cache = {}
  Thread.current[:view_template] = self
  # Define methods for :locals
  (self._options[:locals] || {}).each do |k,v|
    define_instance_method(k) {v}
  end
end

Instance Attribute Details

#_optionsObject

Allows access to any options passed into the template.



8
9
10
# File 'lib/mack/rendering/view_template.rb', line 8

def _options
  @_options
end

#_render_typeObject

Returns the value of attribute _render_type.



9
10
11
# File 'lib/mack/rendering/view_template.rb', line 9

def _render_type
  @_render_type
end

#_render_valueObject

Returns the value of attribute _render_value.



10
11
12
# File 'lib/mack/rendering/view_template.rb', line 10

def _render_value
  @_render_value
end

Instance Method Details

#_app_for_renderingObject

Primarily used by Mack::Rendering::Type::Url when dealing with ‘local’ urls. This returns an instance of the current application to run additional requests through.



120
121
122
123
124
# File 'lib/mack/rendering/view_template.rb', line 120

def _app_for_rendering
  ivar_cache do
    Mack::Utils::Server.build_app
  end
end

#_binderObject

Returns the binding of the current view template to be used with the engines to render.



128
129
130
# File 'lib/mack/rendering/view_template.rb', line 128

def _binder
  binding
end

#_compileObject

Transfers all the instance variables from the controller to the current instance of the view template. This call is cached, so it only happens once, regardless of the number of times it is called.



96
97
98
99
100
101
# File 'lib/mack/rendering/view_template.rb', line 96

def _compile
  ivar_cache("_compiled_template") do
    self._options.symbolize_keys!
    _transfer_vars(self.controller)
  end
end

#_compile_and_renderObject

Fully _compiles and renders the view and, if applicable, it’s layout.



104
105
106
107
108
# File 'lib/mack/rendering/view_template.rb', line 104

def _compile_and_render
  self._compile
  content_for(:view, _render_view)
  _render_layout
end

#concat(txt, b) ⇒ Object

Passes concatenation messages through to the Mack::Rendering::Type object. This should append the text, using the passed in binding, to the final output of the render.



113
114
115
# File 'lib/mack/rendering/view_template.rb', line 113

def concat(txt, b)
  @_render_type.concat(txt, b)
end

#content_for(key, value = nil, &block) ⇒ Object

Stores a string that can be retrieved using yield_to.

Example:

<% content_for(:hello, "Hello World") %>
<%= yield_to :hello %> # => "Hello World"

<% content_for(:bye) do %>
  Ah, it's so sad to say goodbye.
<% end %>
<%= yield_to :bye %> # => "Ah, it's so sad to say goodbye."


88
89
90
91
# File 'lib/mack/rendering/view_template.rb', line 88

def content_for(key, value = nil, &block)
  return @_yield_to_cache[key.to_sym] = value unless value.nil?
  return @_yield_to_cache[key.to_sym] = @_render_type.capture(&block) if block_given?
end

#controllerObject

Allows access to the current Mack::Controller object.



25
26
27
# File 'lib/mack/rendering/view_template.rb', line 25

def controller
  self._options[:controller]
end

#cookiesObject

Returns the Mack::CookieJar associated with the current Mack::Controller object.



40
41
42
# File 'lib/mack/rendering/view_template.rb', line 40

def cookies
  self.controller.cookies
end

#paramsObject

Maps to the controller’s param method. See also Mack::Controller::Base params.



45
46
47
# File 'lib/mack/rendering/view_template.rb', line 45

def params
  self.controller.params
end

#render(render_type, render_value, options = {}) ⇒ Object

Handles rendering calls both in the controller and in the view. For full details of render examples see Mack::Controller render. Although the examples there are all in controllers, they idea is still the same for views.

Examples in the view:

<%= render(:text,  "Hello") %>
<%= render(:action, "show") %>
<%= render(:partial, :latest_news) %>
<%= render(:url, "http://www.mackframework.com") %>


59
60
61
62
# File 'lib/mack/rendering/view_template.rb', line 59

def render(render_type, render_value, options = {})
  options = self._options.merge({:layout => false}).merge(options)
  Mack::Rendering::ViewTemplate.new(render_type, render_value, options)._compile_and_render
end

#requestObject

Returns the Mack::Request associated with the current Mack::Controller object.



30
31
32
# File 'lib/mack/rendering/view_template.rb', line 30

def request
  self.controller.request
end

#sessionObject

Returns the Mack::Session associated with the current Mack::Request.



35
36
37
# File 'lib/mack/rendering/view_template.rb', line 35

def session
  self.request.session
end

#yield_to(key) ⇒ Object

Returns a string stored using content_for.

Example:

<% content_for(:hello, "Hello World") %>
<%= yield_to :hello %> # => "Hello World"

<% content_for(:bye) do %>
  Ah, it's so sad to say goodbye.
<% end %>
<%= yield_to :bye %> # => "Ah, it's so sad to say goodbye."


74
75
76
# File 'lib/mack/rendering/view_template.rb', line 74

def yield_to(key)
  @_yield_to_cache[key.to_sym]
end