Added a parallel to ActionView's capture method (Haml::Helpers#capture_haml).

git-svn-id: svn://hamptoncatlin.com/haml/branches/edge@161 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2006-11-20 06:11:16 +00:00
parent 312b1b078c
commit 53b68c335f
4 changed files with 30 additions and 2 deletions

View File

@ -60,6 +60,22 @@ module Haml
buffer.tabulation -= i
end
# Captures the result of the given block of Haml code and returns
# them as a string. For example, after the following,
#
# - foo = capture_haml(13) do |a|
# %p= a
#
# the local variable <tt>foo</tt> would be assigned to "<p>13</p>\n".
def capture_haml(*args, &block)
buffer_buffer = buffer.buffer
position = buffer_buffer.length
block.call(*args)
buffer_buffer.slice!(position..-1)
end
# Gets a reference to the current Haml::Buffer object.
def buffer # :nodoc:
@haml_stack[-1]

View File

@ -95,5 +95,9 @@ class HelperTest < Test::Unit::TestCase
old_rails = ""
assert(result == new_rails || result == old_rails)
end
def test_capture_haml
assert_equal("\"<p>13</p>\\n\"\n", render("- foo = capture_haml(13) do |a|\n %p= a\n= foo.dump"))
end
end

View File

@ -27,7 +27,11 @@
<p>
reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong
</p>
<div>
<h1>Big!</h1>
<p>Small</p>
<!-- Invisible -->
</div>
<p>baz</p>
<p>boom</p>
foo

View File

@ -12,8 +12,12 @@
- tab_up
%p reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong
- tab_down
= capture_haml do
%div
%h1 Big!
%p Small
/ Invisible
%p baz
- buffer.tabulation = 10
%p boom
- concat "foo\n"