Skip to content

Commit

Permalink
Allow builder-style nesting in nokogiri templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkh committed Sep 13, 2010
1 parent 37fae53 commit b967028
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/tilt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions test/tilt_nokogiritemplate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b967028

Please sign in to comment.