Class ERB

  1. lib/erbextensions.rb
Parent: Object
ERB dot/f_0.png

Methods

public class

  1. process_file

Public class methods

process_file (file_name)

Description:

This method returns the contents of file_name, after expanding all embedded Ruby in the file with ERB. This can be used to “include” an erb file from within another erb file.

Parameters:

file_name
The name of the file to process

Returns:

The contents of file_name, after expanding all embedded Ruby in the file with ERB.

[show source]
# File lib/erbextensions.rb, line 18
  def self.process_file(file_name)
    #
    # ERB does not handle being invoked from within an ERB instance well.
    # In order to make this work,
    # 1.) Pass binding() into result(), in order that the new ERB
    #     instance will create a local variable binding for 'erbout'
    #     rather than using a top-level binding (which would lead to its
    #     clearing any existing text already expanded by ancestor ERB
    #     instances).  This also could be accomplished by using a
    #     unique name instead of 'erbout' (something like Common LISP's
    #     gensym).
    # 2.) Set erb's filename member, so that paths relative to calling files
    #     (the relative gem) will work properly; this also ensures that
    #     stack trace looks reasonable.
    #
    erb = ERB.new(IO.read(file_name), nil, nil, 'erbout')
    erb.filename = file_name
    return erb.result(binding())
  end