Class: Ember::Template
- Inherits:
-
Object
- Object
- Ember::Template
- Defined in:
- lib/ember/template.rb
Defined Under Namespace
Classes: Program
Constant Summary collapse
- @@contexts =
{}
Class Method Summary collapse
-
.load_file(path, options = {}) ⇒ Object
Builds a template whose body is read from the given source.
-
.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.
Instance Method Summary collapse
-
#initialize(input, options = {}) ⇒ Template
constructor
Builds a processor that evaluates eRuby directives in the given input according to the given options.
-
#program ⇒ Object
Ruby source code assembled from the eRuby template provided as input to the constructor of this class.
-
#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.
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.
50 51 52 53 54 |
# File 'lib/ember/template.rb', line 50 def initialize input, = {} @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, = {} path = resolve_path(path, ) new File.read(path), .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, = {} File.read resolve_path(path, ) end |
Instance Method Details
#program ⇒ Object
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 |