Permalink
Browse files

Cached compiled erb templates in procs

  • Loading branch information...
1 parent 1fdc1bf commit d1470105551106d4e0a2f3e65b569b01caf54af6 @josh josh committed Nov 6, 2009
Showing with 13 additions and 4 deletions.
  1. +13 −4 lib/tilt.rb
View
@@ -187,6 +187,11 @@ def template_source
# ERB template implementation. See:
# http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html
class ERBTemplate < Template
+ def initialize(*args)
+ super
+ @template_procs = {}
+ end
+
def compile!
require_template_library 'erb' unless defined?(::ERB)
@engine = ::ERB.new(data, options[:safe], options[:trim], '@_out_buf')
@@ -197,14 +202,11 @@ def template_source
end
def evaluate(scope, locals, &block)
- source, offset = local_assignment_code(locals)
- source = [source, template_source].join("\n")
-
original_out_buf =
scope.instance_variables.any? { |var| var.to_sym == :@_out_buf } &&
scope.instance_variable_get(:@_out_buf)
- scope.instance_eval source, eval_file, line - offset
+ scope.instance_eval(&template_proc(locals))
output = scope.instance_variable_get(:@_out_buf)
scope.instance_variable_set(:@_out_buf, original_out_buf)
@@ -213,6 +215,13 @@ def evaluate(scope, locals, &block)
end
private
+ def template_proc(locals)
+ @template_procs[locals] ||= begin
+ source, offset = local_assignment_code(locals)
+ source = [source, template_source].join("\n")
+ instance_eval("proc { #{source} }", eval_file, line - offset)
+ end
+ end
# ERB generates a line to specify the character coding of the generated
# source in 1.9. Account for this in the line offset.

0 comments on commit d147010

Please sign in to comment.