Class: EZML::Engine

Inherits:
Object show all
Extended by:
Forwardable
Includes:
Util
Defined in:
lib/ezml/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#balance, #check_encoding, #check_ezml_encoding, #contains_interpolation?, #handle_interpolation, #html_safe, #human_indentation, #inspect_obj, #silence_warnings, #unescape_interpolation

Constructor Details

#initialize(template, options = {}) ⇒ Engine

Returns a new instance of Engine.



28
29
30
31
32
33
34
35
36
37
# File 'lib/ezml/engine.rb', line 28

def initialize(template, options = {})
  @options = Options.new(options)

  @template = check_ezml_encoding(template) do |msg, line|
    raise EZML::Error.new(msg, line)
  end

  @template_engine = TemplateEngine.new(options)
  @template_engine.compile(@template)
end

Instance Attribute Details

#indentationObject

Returns the value of attribute indentation.



20
21
22
# File 'lib/ezml/engine.rb', line 20

def indentation
  @indentation
end

#optionsObject

Returns the value of attribute options.



18
19
20
# File 'lib/ezml/engine.rb', line 18

def options
  @options
end

Instance Method Details

#compilerObject

Deprecated



40
41
42
# File 'lib/ezml/engine.rb', line 40

def compiler
  @template_engine
end

#def_method(object, name, *local_names) ⇒ Object



91
92
93
94
95
96
# File 'lib/ezml/engine.rb', line 91

def def_method(object, name, *local_names)
  method = object.is_a?(Module) ? :module_eval : :instance_eval

  object.send(method, "def #{name}(_ezml_locals = {}); #{@template_engine.precompiled_with_ambles(local_names)}; end",
              @options.filename, @options.line)
end

#options_for_bufferObject



24
25
26
# File 'lib/ezml/engine.rb', line 24

def options_for_buffer
  @options.for_buffer
end

#render(scope = Object.new, locals = {}, &block) ⇒ Object Also known as: to_html



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ezml/engine.rb', line 44

def render(scope = Object.new, locals = {}, &block)
  parent = scope.instance_variable_defined?(:@ezml_buffer) ? scope.instance_variable_get(:@ezml_buffer) : nil
  buffer = EZML::Buffer.new(parent, @options.for_buffer)

  if scope.is_a?(Binding)
    scope_object = eval("self", scope)
    scope = scope_object.instance_eval{binding} if block_given?
  else
    scope_object = scope
    scope = scope_object.instance_eval{binding}
  end

  set_locals(locals.merge(:_ezmlout => buffer, :_erbout => buffer.buffer), scope, scope_object)

  scope_object.extend(EZML::Helpers)
  scope_object.instance_variable_set(:@ezml_buffer, buffer)
  begin
    eval(@template_engine.precompiled_with_return_value, scope, @options.filename, @options.line)
  rescue ::SyntaxError => e
    raise SyntaxError, e.message
  end
ensure
  scope_object.instance_variable_set(:@ezml_buffer, buffer.upper) if buffer
end

#render_proc(scope = Object.new, *local_names) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ezml/engine.rb', line 70

def render_proc(scope = Object.new, *local_names)
  if scope.is_a?(Binding)
    scope_object = eval("self", scope)
  else
    scope_object = scope
    scope = scope_object.instance_eval{binding}
  end

  begin
    str = @template_engine.precompiled_with_ambles(local_names)
    eval(
      "Proc.new { |*_ezml_locals| _ezml_locals = _ezml_locals[0] || {}; #{str}}\n",
      scope,
      @options.filename,
      @options.line
    )
  rescue ::SyntaxError => e
    raise SyntaxError, e.message
  end
end