class Kielce::Kielce

Attributes

error_count[R]

Public Class Methods

new(context) click to toggle source

Constructor

@param context default context to use when calling render

# File lib/kielce/kielce.rb, line 31
def initialize(context)
  @data_context = context
  @error_count = 0
  @file_stack = []
end

Public Instance Methods

render(file, local_variables = {}, b = @data_context.instance_exec { binding }) click to toggle source

Load file and run ERB. The binding parameter determines the context the .erb code runs in. The context determines the variables and methods available. By default, kielce uses the same context for loading and rendering. However, users can override this behavior by providing different contexts

@param the file @param b the binding that the template code runs in. @return a String containing the rendered contents

# File lib/kielce/kielce.rb, line 63
def render(file, local_variables = {}, b = @data_context.instance_exec { binding })

  local_variables.each_pair do |key, value|
    b.local_variable_set(key, value)
  end

  # $stderr.puts "In render: #{b.inspect}"
  result = "<!--- ERROR -->"

  begin
    content = File.read(file)
  rescue Errno::ENOENT => e
    # TODO Consider using e.backtrace_locations instead of @file_stack
    # (Question: What exaclty do you want to see if render_relative is called
    # by a method)
    raise IncompleteRenderError.new(file, @file_stack.last, e)
  end
  @file_stack.push(file)

  # The two nil parameters below are legacy settings that don't
  # apply to Kielce.  nil is the default value.  We must specify
  # nil, so we can set the fourth parameter (described below).
  #
  # It is possible for code inside an erb file to load and render
  # another erb template.  In order for such nested calls to work
  # properly, each call must have a unique variable in which to
  # store its output.  This parameter is called "eoutvar". (If you
  # don't specifiy eoutvar and make a nested call, the output
  # can get messed up.)
  @@render_count += 1

  begin
    erb = ERB.new(content, nil, nil, "render_out_#{@@render_count}")
    erb.filename = file.to_s
    result = erb.result(b)
  rescue NoKeyError => e
    line_num = e.backtrace_locations.select { |i| i.path == file }.first.lineno
    $stderr.puts "Unrecognized key #{e.name} at #{file}:#{line_num}"
    @error_count += 1
  ensure
    @file_stack.pop
  end
  result
end
render_relative(file, local_variables = {}, b = @data_context.instance_exec { binding }) click to toggle source
# File lib/kielce/kielce.rb, line 108
def render_relative(file, local_variables = {}, b = @data_context.instance_exec { binding })
  path = Pathname.new(File.absolute_path(@file_stack.last)).dirname
  render(path.join(file), local_variables, b)
end