mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
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
4 changed files with 30 additions and 2 deletions
|
@ -59,6 +59,22 @@ module Haml
|
|||
def tab_down(i = 1)
|
||||
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:
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -27,7 +27,11 @@
|
|||
<p>
|
||||
reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong
|
||||
</p>
|
||||
<div>
|
||||
<h1>Big!</h1>
|
||||
<p>Small</p>
|
||||
<!-- Invisible -->
|
||||
</div>
|
||||
<p>baz</p>
|
||||
<p>boom</p>
|
||||
foo
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
Loading…
Reference in a new issue