0
@@ -5,38 +5,84 @@ module Webby::Helpers
0
+ # Called in pages and partials to store up content for later use. Takes a
0
+ # string and/or a block. First, the string is evaluated, and then the
0
+ # block is captured using the capture() helper provided by the template
0
+ # languages. The two are concatenated together.
0
+ # Content is retrieved by calling the method without a string or a block.
0
+ # obj<Object>:: The key in the conetnt_for hash.
0
+ # string<String>:: Textual content. Defaults to nil.
0
+ # &block:: A block to be evaluated and concatenated to string.
0
+ # Any content associated with the key (or nil).
0
+ # content_for(:foo, "Foo")
0
+ # content_for(:foo) #=> "Foo"
0
+ # content_for(:foo, "Bar")
0
+ # content_for(:foo) #=> "FooBar"
0
+ def content_for( obj, string = nil, &block )
0
+ return @_content_for[obj] unless string || block_given?
0
- def content_for( name, content = nil, &block )
0
- cur = instance_variable_get("@content_for_#{name}").to_s
0
- new = if block.nil? then content
0
- else capture_erb(&block) end
0
+ cur = @_content_for[obj].to_s
0
+ new = string.to_s + (block_given? ? capture_erb(&block) : "")
0
+ @_content_for[obj] = cur + new
0
- instance_variable_set("@content_for_#{name}", new)
0
+ # Returns true if there is content for the given key. Otherwise returns
0
+ # obj<Object>:: The key in the conetnt_for hash.
0
+ # content_for(:foo, "Foo")
0
+ # content_for?(:foo) #=> true
0
+ # content_for?(:bar) #=> false
0
+ def content_for?( obj )
0
+ @_content_for.key?(obj)
0
- # Provides direct acccess to the buffer for this view context
0
+ # Deletes any content associated with the given object in the content_for
0
- #
the_binding<Binding>:: The binding to pass to the buffer.
0
+ #
obj<Object>:: The key in the conetnt_for hash.
0
- def _erb_buffer( the_binding )
0
- eval("_erbout", the_binding, __FILE__, __LINE__)
0
+ # Any content associated with the key (or nil).
0
+ # content_for(:foo, "Foo")
0
+ # content_for?(:foo) #=> true
0
+ # delete_content_for(:foo)
0
+ # content_for?(:foo) #=> false
0
+ def delete_content_for( obj )
0
+ @_content_for.delete(obj)
0
+ # This method is used to capture content from an ERB filter evaluation. It
0
+ # is useful to helpers that need to process chunks of data during ERB filter
0
# *args:: Arguments to pass to the block.
0
- # &block:: The
template block to call.
0
+ # &block:: The
ERB block to call.
0
# String:: The output of the block.
0
- # Capture being used in a
.html.erb page:
0
+ # Capture being used in a
n ERB page:
0
- # <% @foo = capture
do %>
0
+ # <% @foo = capture
_erb do %>
0
# <p>Some Foo content!</p>
0
@@ -56,17 +102,36 @@ module CaptureHelper
0
# replace it in the original with empty string
0
- def concat_erb(string, the_binding)
0
+ # This method is used to concatenate content into the ERB output buffer.
0
+ # It is usefule to helpers that need to insert transformed text back into
0
+ # the ERB output buffer.
0
+ # string<String>:: The string to insert into the ERB output.
0
+ # the_binding<Binding>:: The binding to pass to the buffer.
0
+ def concat_erb( string, the_binding )
0
_erb_buffer(the_binding) << string
0
+ # Provides direct acccess to the ERB buffer in the conext of the binding.
0
+ # the_binding<Binding>:: The binding to pass to the buffer.
0
+ # The current ERB output buffer.
0
+ def _erb_buffer( the_binding )
0
+ eval("_erbout", the_binding, __FILE__, __LINE__)
0
end # module CaptureHelper
0
register(CaptureHelper)
Comments
No one has commented yet.