Skip to content

Commit

Permalink
Cached compiled erb templates in procs
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Nov 6, 2009
1 parent 1fdc1bf commit d147010
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/tilt.rb
Expand Up @@ -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')
Expand All @@ -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)
Expand All @@ -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.
Expand Down

0 comments on commit d147010

Please sign in to comment.