Method: Haml::Helpers#capture_haml
- Defined in:
- lib/haml/helpers.rb
#capture_haml(*args) {|args| ... }
Captures the result of a block of Haml code, gets rid of the excess indentation, and returns it as a string. For example, after the following,
.foo
- foo = capture_haml(13) do |a|
%p= a
the local variable foo would be assigned to "<p>13</p>\n".
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/haml/helpers.rb', line 362
def capture_haml(*args, &block)
buffer = eval('if defined? _hamlout then _hamlout else nil end', block.binding) || haml_buffer
with_haml_buffer(buffer) do
position = haml_buffer.buffer.length
haml_buffer.capture_position = position
block.call(*args)
captured = haml_buffer.buffer.slice!(position..-1)
return captured if haml_buffer.options[:ugly]
# Note that the "reject" is needed for rbx 1.2.4, which includes empty
# strings in the returned array when splitting by /^/.
captured = captured.split(/^/).reject {|x| x == ""}
min_tabs = nil
captured.each do |line|
tabs = line.index(/[^ ]/) || line.length
min_tabs ||= tabs
min_tabs = min_tabs > tabs ? tabs : min_tabs
end
captured.map do |line|
line[min_tabs..-1]
end.join
end
ensure
haml_buffer.capture_position = nil
end
|