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:
parent
312b1b078c
commit
53b68c335f
|
@ -60,6 +60,22 @@ module Haml
|
||||||
buffer.tabulation -= i
|
buffer.tabulation -= i
|
||||||
end
|
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.
|
# Gets a reference to the current Haml::Buffer object.
|
||||||
def buffer # :nodoc:
|
def buffer # :nodoc:
|
||||||
@haml_stack[-1]
|
@haml_stack[-1]
|
||||||
|
|
|
@ -95,5 +95,9 @@ class HelperTest < Test::Unit::TestCase
|
||||||
old_rails = ""
|
old_rails = ""
|
||||||
assert(result == new_rails || result == old_rails)
|
assert(result == new_rails || result == old_rails)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,11 @@
|
||||||
<p>
|
<p>
|
||||||
reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong
|
reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong
|
||||||
</p>
|
</p>
|
||||||
|
<div>
|
||||||
|
<h1>Big!</h1>
|
||||||
|
<p>Small</p>
|
||||||
|
<!-- Invisible -->
|
||||||
|
</div>
|
||||||
<p>baz</p>
|
<p>baz</p>
|
||||||
<p>boom</p>
|
<p>boom</p>
|
||||||
foo
|
foo
|
||||||
|
|
||||||
|
|
|
@ -12,8 +12,12 @@
|
||||||
- tab_up
|
- tab_up
|
||||||
%p reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong
|
%p reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong
|
||||||
- tab_down
|
- tab_down
|
||||||
|
= capture_haml do
|
||||||
|
%div
|
||||||
|
%h1 Big!
|
||||||
|
%p Small
|
||||||
|
/ Invisible
|
||||||
%p baz
|
%p baz
|
||||||
- buffer.tabulation = 10
|
- buffer.tabulation = 10
|
||||||
%p boom
|
%p boom
|
||||||
- concat "foo\n"
|
- concat "foo\n"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue