Class: ERBB::Parser

Inherits:
ERB
  • Object
show all
Defined in:
lib/erbb/parser.rb

Overview

Parses ERBB files (erb files with #named_block calls in them). Use like you would the ERB class, except instead of passing a binding when getting the result, you pass in an object which will be the implicit receiver for all ruby calls made in the erb template. This object will be decorated with the instance methods in ERBB::Receiver when rendering.

Instance Method Summary collapse

Constructor Details

#initialize(str, safe_level = nil, trim_mode = '>', eoutvar = nil) ⇒ Parser

Returns a new instance of Parser.

Parameters:

  • str (String)

    the template to render.

  • safe_level (Nil, Integer) (defaults to: nil)

    the safe level.

  • trim_mode (Nil, String) (defaults to: '>')

    how to handle newlines.

  • eoutvar (Nil) (defaults to: nil)

    if the user passes a 4th arg, error

Raises:

  • (ArgumentError)

See Also:



13
14
15
16
17
18
19
# File 'lib/erbb/parser.rb', line 13

def initialize(str, safe_level=nil, trim_mode='>', eoutvar=nil)
  # The fourth arg is the name of the variable defined on the receiver
  # which is used to store the rendered output when parsing a template.
  # ERBB works by exploiting that, so it has to control it.
  raise ArgumentError, "In ERBB, the template ivar can’t be set" if eoutvar
  super(str, safe_level, trim_mode, Receiver::RENDERED_TEMPLATE)
end

Instance Method Details

#result(b = new_toplevel) ⇒ ERBB::Result

Returns a string decorated with named blocks.

Parameters:

  • b (Binding) (defaults to: new_toplevel)

    the binding to use for rendering.

Returns:

See Also:



24
25
26
27
28
29
30
31
32
33
# File 'lib/erbb/parser.rb', line 24

def result(b=new_toplevel)
  receiver = extract_receiver_from_binding(b)

  # Render the template using the binding and return the result along with
  #   the output of any named blocks.
  ERBB::Result.new(
    super(b),
    receiver.named_blocks
  )
end