Class: Pancake::Mixins::Render::ViewContext

Inherits:
Object
  • Object
show all
Includes:
AnyView::TiltBase, Tilt::CompileSite
Defined in:
lib/pancake/mixins/render/view_context.rb

Defined Under Namespace

Classes: InheritanceHelper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(renderer_for = nil, opts = {}) ⇒ ViewContext

Returns a new instance of ViewContext.



34
35
36
37
38
39
40
# File 'lib/pancake/mixins/render/view_context.rb', line 34

def initialize(renderer_for = nil, opts = {})
  opts.keys.each do |k,v|
    instance_variable_set("@#{k}", v)
  end
  @_inherit_helper = InheritanceHelper.new
  @_view_context_for = renderer_for
end

Instance Attribute Details

#_view_context_forObject (readonly)

Returns the value of attribute _view_context_for.



32
33
34
# File 'lib/pancake/mixins/render/view_context.rb', line 32

def _view_context_for
  @_view_context_for
end

Class Method Details

.capture_method_for(item) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/pancake/mixins/render/view_context.rb', line 11

def capture_method_for(item)
  key = case item
        when Template
          item.renderer.class
        when Tilt::Template
          item
        end
  AnyView::TiltBase.capture_methods[key]
end

.concat_method_for(item) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/pancake/mixins/render/view_context.rb', line 21

def concat_method_for(item)
  key = case item
        when Template
          item.renderer.class
        when Tilt::Template
          item
        end
  AnyView::TiltBase.concat_methods[key]
end

Instance Method Details

#_current_rendererObject



121
122
123
# File 'lib/pancake/mixins/render/view_context.rb', line 121

def _current_renderer
  @_current_renderer
end

#_with_renderer(renderer) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/pancake/mixins/render/view_context.rb', line 113

def _with_renderer(renderer)
  orig_renderer = @_current_renderer
  @_current_renderer = renderer
  result = yield
  @_current_renderer = orig_renderer
  result
end

#content_block(label = nil, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pancake/mixins/render/view_context.rb', line 71

def content_block(label = nil, &block)
  return self if label.nil?
  current_label = @_inherit_helper.current_label
  @_inherit_helper.current_label = label
  capture_method = ViewContext.capture_method_for(_current_renderer)

  @_inherit_helper.blocks[label] << [block, capture_method]
  if @_inherit_helper.inherits_from.nil?
    result = _capture_content_block(label)
    send(ViewContext.concat_method_for(_current_renderer), result)
  end
  @_inherit_helper.current_label = current_label
end

#inherits_from(ntos, name_or_opts = nil, opts = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pancake/mixins/render/view_context.rb', line 42

def inherits_from(ntos, name_or_opts = nil, opts = {})
  name_or_template = case ntos
  when String, Symbol
    if ntos == :default!
      begin
        if @format
          Pancake.default_base_template(:format => @format)
        else
          Pancake.default_base_template
        end
      rescue
        :base
      end
    else
      ntos
    end
  when Pancake::Mixins::Render::Template
    ntos
  else
    if name_or_opts.kind_of?(Hash)
      opts = name_or_opts
      name_or_opts = nil
    end
    name_or_opts ||= ntos.base_template_name
    ntos.template(name_or_opts, opts)
  end
  @_inherit_helper.inherits_from = name_or_template
end

#partial(*args) ⇒ Object



109
110
111
# File 'lib/pancake/mixins/render/view_context.rb', line 109

def partial(*args)
  _view_context_for.partial(*args)
end

#render(template, opts = {}, &blk) ⇒ Object

Raises:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/pancake/mixins/render/view_context.rb', line 92

def render(template, opts = {}, &blk)
  template = _view_context_for.template(template)
  raise TemplateNotFound unless template
  result = _with_renderer template do
    _current_renderer.render(self, opts, &blk) # only include the block once
  end

  if @_inherit_helper.inherits_from
    next_template = template.owner.template(@_inherit_helper.inherits_from)
    @_inherit_helper.inherits_from = nil
    result = _with_renderer next_template do
      render(next_template, opts)
    end
  end
  result
end

#superObject



85
86
87
88
89
90
# File 'lib/pancake/mixins/render/view_context.rb', line 85

def super
  @_inherit_helper.increment_super!
  result = _capture_content_block(@_inherit_helper.current_label)
  @_inherit_helper.decrement_super!
  result
end