Permalink
Browse files

Allow builder-style nesting in nokogiri templates.

  • Loading branch information...
1 parent 37fae53 commit b967028db6ffc791799330fc693436ef449f6f84 @rkh rkh committed Sep 13, 2010
Showing with 6 additions and 7 deletions.
  1. +2 −1 lib/tilt.rb
  2. +4 −6 test/tilt_nokogiritemplate_test.rb
View
@@ -632,9 +632,10 @@ def initialize_engine
def prepare; end
def evaluate(scope, locals, &block)
- xml = locals[:xml] || ::Nokogiri::XML::Builder.new
+ xml = ::Nokogiri::XML::Builder.new
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)
@@ -49,13 +49,11 @@ class NokogiriTemplateTest < Test::Unit::TestCase
assert_equal 'em', doc.root.name
end
- test "passing in xml builder instance to allow nesting" do
+ test "allows nesting raw XML, API-compatible to Builder" do
subtemplate = Tilt::NokogiriTemplate.new { "xml.em 'Hello World!'" }
- template =
- Tilt::NokogiriTemplate.new do |t|
- lambda { |x| x.strong { subtemplate.render Object.new, :xml => x }}
- end
- doc = Nokogiri.XML template.render
+ template = Tilt::NokogiriTemplate.new { "xml.strong { xml << yield }" }
+ options = { :xml => Nokogiri::XML::Builder.new }
+ doc = Nokogiri.XML(template.render(options) { subtemplate.render(options) })
assert_equal 'Hello World!', doc.root.text.strip
assert_equal 'strong', doc.root.name
end

0 comments on commit b967028

Please sign in to comment.