Class: Docks::Renderers::ERB

Inherits:
Base
  • Object
show all
Includes:
Common::Capturable, Common::Helperable
Defined in:
lib/docks/renderers/erb_renderer.rb

Instance Method Summary collapse

Methods included from Common::Capturable

#content_for, #content_for?

Methods included from Common::Helperable

#helpers

Methods inherited from Base

#ivars=

Constructor Details

#initializeERB

Returns a new instance of ERB.



11
12
13
14
15
16
17
# File 'lib/docks/renderers/erb_renderer.rb', line 11

def initialize
  require "erb"

  super
  @locals = []
  @output = ""
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *arguments, &block) ⇒ Object



53
54
55
56
57
58
# File 'lib/docks/renderers/erb_renderer.rb', line 53

def method_missing(meth, *arguments, &block)
  super if @locals.empty?
  @locals.last.fetch(meth)
rescue KeyError
  super
end

Instance Method Details

#capture(*args) {|args| ... } ⇒ Object

Yields:

  • (args)


41
42
43
44
45
46
47
# File 'lib/docks/renderers/erb_renderer.rb', line 41

def capture(*args, &block)
  old_output, @output = @output, ""
  yield *args
  content = @output
  @output = old_output
  content
end

#concat(content) ⇒ Object



49
50
51
# File 'lib/docks/renderers/erb_renderer.rb', line 49

def concat(content)
  @output << content
end

#get_bindingObject



37
38
39
# File 'lib/docks/renderers/erb_renderer.rb', line 37

def get_binding
  binding
end

#render(template, locals = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/docks/renderers/erb_renderer.rb', line 19

def render(template, locals = {})
  first_pass = @output.length < 1
  final_output, @output = @output, ""

  content, layout, locals = normalize_content_and_locals(template, locals)
  return if content.nil?

  @locals << locals
  content = ::ERB.new(content, nil, nil, "@output").result(get_binding)
  return content if layout.nil?

  ::ERB.new(layout, nil, nil, "@output").result(get_binding { |name| name.nil? ? content : @content_blocks[name] })

ensure
  @locals.pop
  @output = first_pass ? "" : final_output
end