Sha256: b8c37373ec05cb63443bdee9aa2f2196c9d21d151e946d0a382e44f4c3104aff

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

#--
# Copyright 2006 Suraj N. Kurapati
# See the file named LICENSE for details.

require 'erb'

# Returns an array containing the current ERB buffer and the content that the
# given block will append to the buffer when it is invoked.
#
# == Example
# Suppose your ERB template invoked a method with some arguments and some
# content in a block. You can pass the block to this method to obtain the
# content contained within the block.
#
## template = ERB.new <<-EOS
## <% wrap_xml "message" do %>
##   i love ruby!
## <% end %>
## EOS
#
# In this case, the ERB template invokes the _wrap_xml_ method to wrap some
# content within a pair of XML tags.
#
## def wrap_xml tag, &block
##   buffer, content = ERB.buffer_and_content(&block)
##   buffer << "<#{tag}>#{content}</#{tag}>"
## end
#
# When we evaluate the template:
## puts template.result(binding)
#
# we see the following output:
## <message>
##   i love ruby!
## </message>
#
def ERB.buffer_and_content
  raise ArgumentError unless block_given?

  # buffer + content
  buffer = yield
  a = buffer.length

  # buffer + content + content
  yield
  b = buffer.length

  # buffer + content
  content = buffer.slice! a..b

  # buffer
  buffer.slice!((-content.length)..-1)

  [buffer, content]
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-vpi-16.0.1 doc/lib/erb_content.rb
ruby-vpi-16.0.0 doc/lib/erb_content.rb