Class: Mack::Rendering::Engine::Erubis

Inherits:
Base show all
Defined in:
lib/mack/rendering/engine/erubis.rb

Overview

Allows use of the Builder::XmlMarkup engine to be used with rendering.

Instance Attribute Summary

Attributes inherited from Base

#view_template

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Mack::Rendering::Engine::Base

Instance Method Details

#capture(*args, &block) ⇒ Object

See Mack::Rendering::ViewTemplate content_for for more details. Thanks Merb.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mack/rendering/engine/erubis.rb', line 50

def capture(*args, &block)
  # get the buffer from the block's binding
  buffer = _erb_buffer( block.binding ) rescue nil

  # If there is no buffer, just call the block and get the contents
  if buffer.nil?
    block.call(*args)
  # If there is a buffer, execute the block, then extract its contents
  else
    pos = buffer.length
    block.call(*args)

    # extract the block
    data = buffer[pos..-1]

    # replace it in the original with empty string
    buffer[pos..-1] = ''

    data
  end
end

#concat(txt, b) ⇒ Object



44
45
46
# File 'lib/mack/rendering/engine/erubis.rb', line 44

def concat(txt, b)
  eval( "_buf", b) << txt
end

#extensionObject



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

def extension
  :erb
end

#render(io, binding) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mack/rendering/engine/erubis.rb', line 8

def render(io, binding)
  io_src = io
  file_name = nil
  if io.is_a?(File)
    io_src = io.read
    file_name = io.path
  end
  
  eruby = nil

  eruby = ::Erubis::Eruby.new(io_src)

  eruby.filename = file_name
  begin
    eruby.result(binding)
  rescue NoMethodError => e
    if file_name
      m = NoMethodError.new("undefined method `#{e.name}' for #{e.backtrace[0].match(/(^.+:\d+)/).captures.first}")
      m.set_backtrace(e.backtrace)
      raise m
    end
    raise e
  rescue NameError => e
    if file_name
      m = NameError.new("undefined local variable or method `#{e.name}' for #{e.backtrace[0].match(/(^.+:\d+)/).captures.first}")
      m.set_backtrace(e.backtrace)
      raise m
    end
    raise e
  end
end