Class: Ember::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/ember/template.rb

Defined Under Namespace

Classes: Program

Constant Summary collapse

@@contexts =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, options = {}) ⇒ Template

Builds a processor that evaluates eRuby directives in the given input according to the given options.

This processor transforms the given input into an executable Ruby program (provided by the #program method) which is then executed by the #render method on demand.

eRuby directives that contribute to the output of the given template are called “vocal” directives. Those that do not are called “silent” directives.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :result_variable (String) — default: "_erbout"

    Name of the variable which stores the result of template evaluation during template evaluation.

  • :continue_result (Boolean) — default: false

    Append to the result variable if it already exists?

  • :source_file (String) — default: "SOURCE"

    Name of the file which contains the given input. This is shown in stack traces when reporting error messages.

  • :source_line (Integer) — default: 1

    Line number at which the given input exists in the :source_file. This is shown in stack traces when reporting error messages.

  • :shorthand (Boolean) — default: false

    Treat lines beginning with “%” as eRuby directives?

  • :infer_end (Boolean) — default: false

    Add missing <% end %> statements based on indentation?

  • :unindent (Boolean) — default: false

    Unindent the content of eRuby blocks—that is everything between the <% do %> and <% end %> tags—hierarchically?



50
51
52
53
54
# File 'lib/ember/template.rb', line 50

def initialize input, options = {}
  @options = options
  @render_context_id = object_id
  @compile = compile(input.to_s)
end

Class Method Details

.load_file(path, options = {}) ⇒ Object

Builds a template whose body is read from the given source.

If the source is a relative path, it will be resolved relative to options if that is a valid path.



91
92
93
94
# File 'lib/ember/template.rb', line 91

def load_file path, options = {}
  path = resolve_path(path, options)
  new File.read(path), options.merge(:source_file => path)
end

.read_file(path, options = {}) ⇒ Object

Returns the contents of the given file, which can be relative to the current template in which this command is being executed.

If the source is a relative path, it will be resolved relative to options if that is a valid path.



103
104
105
# File 'lib/ember/template.rb', line 103

def read_file path, options = {}
  File.read resolve_path(path, options)
end

Instance Method Details

#programObject

Ruby source code assembled from the eRuby template provided as input to the constructor of this class.



60
61
62
# File 'lib/ember/template.rb', line 60

def program
  @compile
end

#render(context = nil, parent_context_id = nil) ⇒ Object

Returns the result of executing the Ruby program for this template (provided by the #program method) inside the given context binding.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ember/template.rb', line 70

def render context = nil, parent_context_id = nil
  context ||=
    @@contexts[parent_context_id] ||        # inherit parent context
    Object.new.instance_eval('binding')     # create new context
  @@contexts[@render_context_id] = context  # provide to children

  result = eval @compile, context,
    (@options[:source_file] || :SOURCE).to_s,
    (@options[:source_line] || 1).to_i

  @@contexts.delete @render_context_id      # free the memory
  result
end