Action View Text Template
- MODULE ActionView::Template::Handlers
- CLASS ActionView::Template::Error
- CLASS ActionView::Template::Text
Finalizer | = | proc do |method_name, mod| proc do mod.module_eval do remove_possible_method method_name end end end |
This finalizer is needed (and exactly with a proc inside another proc) otherwise templates leak in development. |
[RW] | locals | |
[RW] | formats | |
[RW] | virtual_path | |
[R] | source | |
[R] | identifier | |
[R] | handler | |
[R] | original_encoding | |
[R] | updated_at |
# File actionpack/lib/action_view/template.rb, line 115 115: def initialize(source, identifier, handler, details) 116: format = details[:format] || (handler.default_format if handler.respond_to?(:default_format)) 117: 118: @source = source 119: @identifier = identifier 120: @handler = handler 121: @compiled = false 122: @original_encoding = nil 123: @locals = details[:locals] || [] 124: @virtual_path = details[:virtual_path] 125: @updated_at = details[:updated_at] || Time.now 126: @formats = Array.wrap(format).map { |f| f.is_a?(Mime::Type) ? f.ref : f } 127: @compile_mutex = Mutex.new 128: end
This method is responsible for properly setting the encoding of the source. Until this point, we assume that the source is BINARY data. If no additional information is supplied, we assume the encoding is the same as Encoding.default_external.
The user can also specify the encoding via a comment on the first line of the template (# encoding: NAME-OF-ENCODING). This will work with any template engine, as we process out the encoding comment before passing the source on to the template engine, leaving a blank line in its stead.
# File actionpack/lib/action_view/template.rb, line 188 188: def encode! 189: return unless source.encoding_aware? && source.encoding == Encoding::BINARY 190: 191: # Look for # encoding: *. If we find one, we'll encode the 192: # String in that encoding, otherwise, we'll use the 193: # default external encoding. 194: if source.sub!(/\A#{ENCODING_FLAG}/, '') 195: encoding = magic_encoding = $1 196: else 197: encoding = Encoding.default_external 198: end 199: 200: # Tag the source with the default external encoding 201: # or the encoding specified in the file 202: source.force_encoding(encoding) 203: 204: # If the user didn't specify an encoding, and the handler 205: # handles encodings, we simply pass the String as is to 206: # the handler (with the default_external tag) 207: if !magic_encoding && @handler.respond_to?(:handles_encoding?) && @handler.handles_encoding? 208: source 209: # Otherwise, if the String is valid in the encoding, 210: # encode immediately to default_internal. This means 211: # that if a handler doesn't handle encodings, it will 212: # always get Strings in the default_internal 213: elsif source.valid_encoding? 214: source.encode! 215: # Otherwise, since the String is invalid in the encoding 216: # specified, raise an exception 217: else 218: raise WrongEncodingError.new(source, encoding) 219: end 220: end
Receives a view object and return a template similar to self by using @virtual_path.
This method is useful if you have a template object but it does not contain its source anymore since it was already compiled. In such cases, all you need to do is to call refresh passing in the view object.
Notice this method raises an error if the template to be refreshed does not have a virtual path set (true just for inline templates).
# File actionpack/lib/action_view/template.rb, line 163 163: def refresh(view) 164: raise "A template needs to have a virtual path in order to be refreshed" unless @virtual_path 165: lookup = view.lookup_context 166: pieces = @virtual_path.split("/") 167: name = pieces.pop 168: partial = !!name.sub!(/^_/, "") 169: lookup.disable_cache do 170: lookup.find_template(name, [ pieces.join('/') ], partial, @locals) 171: end 172: end
Render a template. If the template was not compiled yet, it is done exactly before rendering.
This method is instrumented as “!render_template.action_view“. Notice that we use a bang in this instrumentation because you don’t want to consume this in production. This is only slow if it’s being listened to.
# File actionpack/lib/action_view/template.rb, line 142 142: def render(view, locals, buffer=nil, &block) 143: ActiveSupport::Notifications.instrument("!render_template.action_view", :virtual_path => @virtual_path) do 144: compile!(view) 145: view.send(method_name, locals, buffer, &block) 146: end 147: rescue Exception => e 148: handle_render_error(view, e) 149: end
Returns if the underlying handler supports streaming. If so, a streaming buffer may be passed when it start rendering.