lib/tilt.rb in tilt-1.2.1 vs lib/tilt.rb in tilt-1.2.2
- old
+ new
@@ -1,8 +1,8 @@
module Tilt
TOPOBJECT = defined?(BasicObject) ? BasicObject : Object
- VERSION = '1.2.1'
+ VERSION = '1.2.2'
@template_mappings = {}
# Hash of template path pattern => template implementation class mappings.
def self.mappings
@@ -162,20 +162,24 @@
compile!
else
raise NotImplementedError
end
end
+
+ def evaluate(scope, locals, &block)
+ cached_evaluate(scope, locals, &block)
+ end
# Process the template and return the result. The first time this
# method is called, the template source is evaluated with instance_eval.
# On the sequential method calls it will compile the template to an
# unbound method which will lead to better performance. In any case,
# template executation is guaranteed to be performed in the scope object
# with the locals specified and with support for yielding to the block.
- def evaluate(scope, locals, &block)
+ def cached_evaluate(scope, locals, &block)
# Redefine itself to use method compilation the next time:
- def self.evaluate(scope, locals, &block)
+ def self.cached_evaluate(scope, locals, &block)
method = compiled_method(locals.keys)
method.bind(scope).call(locals, &block)
end
# Use instance_eval the first time:
@@ -618,23 +622,30 @@
return if defined?(::Nokogiri)
require_template_library 'nokogiri'
end
def prepare; end
-
+
def evaluate(scope, locals, &block)
- xml = ::Nokogiri::XML::Builder.new
+ block &&= proc { yield.gsub(/^<\?xml version=\"1\.0\"\?>\n?/, "") }
+
if data.respond_to?(:to_str)
- locals[:xml] = xml
- block &&= proc { yield.gsub(/^<\?xml version=\"1\.0\"\?>\n?/, "") }
super(scope, locals, &block)
- elsif data.kind_of?(Proc)
- data.call(xml)
+ else
+ ::Nokogiri::XML::Builder.new.tap(&data).to_xml
end
- xml.to_xml
end
+ def precompiled_preamble(locals)
+ return super if locals.include? :xml
+ "xml = ::Nokogiri::XML::Builder.new\n#{super}"
+ end
+
+ def precompiled_postamble(locals)
+ "xml.to_xml"
+ end
+
def precompiled_template(locals)
data.to_str
end
end
register 'nokogiri', NokogiriTemplate
@@ -645,21 +656,25 @@
def initialize_engine
return if defined?(::Builder)
require_template_library 'builder'
end
- def prepare
- end
+ def prepare; end
def evaluate(scope, locals, &block)
+ return super(scope, locals, &block) if data.respond_to?(:to_str)
xml = ::Builder::XmlMarkup.new(:indent => 2)
- if data.respond_to?(:to_str)
- locals[:xml] = xml
- super(scope, locals, &block)
- elsif data.kind_of?(Proc)
- data.call(xml)
- end
+ data.call(xml)
xml.target!
+ end
+
+ def precompiled_preamble(locals)
+ return super if locals.include? :xml
+ "xml = ::Builder::XmlMarkup.new(:indent => 2)\n#{super}"
+ end
+
+ def precompiled_postamble(locals)
+ "xml.target!"
end
def precompiled_template(locals)
data.to_str
end