Class: Ember::Template
- Inherits:
-
Object
- Object
- Ember::Template
- Defined in:
- lib/ember/template.rb
Defined Under Namespace
Classes: Program
Class Method Summary collapse
-
.buffer_from_block(content_block) ⇒ Object
Returns the template evaluation result buffer associated with the given content block.
-
.content_from_block(content_block, *content_block_args) ⇒ Object
Invokes the given block while passing the given arguments to it and then returns an Array of things that the given block tried to append to its template evaluation result buffer.
-
.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.
-
.wrap_content_block(content_block, *content_block_args, &wrapper) ⇒ Object
Passes the given content block’s content (obtained by invoking the given content block with the given arguments) as an argument to given wrapper block.
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 = Object.new.instance_eval('binding')) ⇒ 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
.buffer_from_block(content_block) ⇒ Object
Returns the template evaluation result buffer associated with the given content block.
101 102 103 104 105 |
# File 'lib/ember/template.rb', line 101 def buffer_from_block content_block context = content_block.binding result_variable = eval(Program::RESULT_VARIABLE_BACKDOOR, context) eval result_variable.to_s, context end |
.content_from_block(content_block, *content_block_args) ⇒ Object
Invokes the given block while passing the given arguments to it and then returns an Array of things that the given block tried to append to its template evaluation result buffer.
If the given block did not try to append anything, then the result of invoking the given block is returned as an Array.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/ember/template.rb', line 115 def content_from_block content_block, *content_block_args context = content_block.binding orig_result_variable = eval(Program::RESULT_VARIABLE_BACKDOOR, context) temp_result_variable = "#{orig_result_variable}_#{context.object_id.abs}" eval "#{temp_result_variable} = #{orig_result_variable}", context begin # the content block appends to the result buffer when invoked. # so we temporarily replace the result buffer with an empty one, # invoke the content block, and viola! we now have its content. block_content = eval("#{orig_result_variable} = []", context) return_value = content_block.call(*content_block_args) ensure eval "#{orig_result_variable} = #{temp_result_variable}", context end if block_content.empty? # no content was appended to the result buffer when the content # block was invoked because it did not contain any vocal directives. # so we return the return value of content block invocation instead. Array(return_value) else block_content end end |
.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.
81 82 83 84 |
# File 'lib/ember/template.rb', line 81 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.
93 94 95 |
# File 'lib/ember/template.rb', line 93 def read_file path, = {} File.read resolve_path(path, ) end |
.wrap_content_block(content_block, *content_block_args, &wrapper) ⇒ Object
Passes the given content block’s content (obtained by invoking the given content block with the given arguments) as an argument to given wrapper block. The result of the wrapper block is (1) appended to the template evaluation result buffer associated with the given content block and is (2) returned by this method.
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/ember/template.rb', line 149 def wrap_content_block content_block, *content_block_args, &wrapper raise ArgumentError, 'wrapper block must be given' unless wrapper buffer = buffer_from_block(content_block) content = content_from_block(content_block, *content_block_args) wrapped_content = wrapper.call(content) buffer << wrapped_content wrapped_content 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 = Object.new.instance_eval('binding')) ⇒ Object
Returns the result of executing the Ruby program for this template (provided by the #program method) inside the given context binding.
68 69 70 71 72 |
# File 'lib/ember/template.rb', line 68 def render context = Object.new.instance_eval('binding') eval @compile, context, (@options[:source_file] || :SOURCE).to_s, (@options[:source_line] || 1).to_i end |