Sha256: ea004271b2a61776b78c37c009c510f3fdd8eef25f9d50e743a638ecf32f3402

Contents?: true

Size: 1.6 KB

Versions: 5

Compression:

Stored size: 1.6 KB

Contents

module Merb
  
  

  module ErubisCaptureMixin
    
    # Provides direct acccess to the buffer for this view context
    def _buffer( the_binding )
      @_buffer = eval( "_buf", the_binding )
    end
    
    # Capture allows you to extract a part of the template into an 
    # instance variable. You can use this instance variable anywhere
    # in your templates and even in your layout. 
    # 
    # Example of capture being used in a .herb page:
    # 
    #   <% @foo = capture do %>
    #     <p>Some Foo content!</p> 
    #   <% end %>
    def capture(*args, &block)
      # execute the block
      begin
        buffer = _buffer( block.binding )
      rescue
        buffer = nil
      end
      
      if buffer.nil?
        capture_block(*args, &block)
      else
        capture_erb_with_buffer(buffer, *args, &block)
      end
    end
        
    private
      def capture_block(*args, &block)
        block.call(*args)
      end
    
      def capture_erb(*args, &block)
        buffer = _buffer
        capture_erb_with_buffer(buffer, *args, &block)
      end
    
      def capture_erb_with_buffer(buffer, *args, &block)
        pos = buffer.length
        block.call(*args)
      
        # extract the block 
        data = buffer[pos..-1]
      
        # replace it in the original with empty string
        buffer[pos..-1] = ''
      
        data
      end
    
      def erb_content_for(name, &block)
        controller.thrown_content[name] << capture_erb( &block )
      end
    
      def block_content_for(name, &block)
        controller.thrown_content[name] << capture_block( &block )
      end
  end
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
merb-0.5.0 lib/merb/mixins/erubis_capture.rb
merb-0.4.2 lib/merb/mixins/erubis_capture.rb
merb-0.5.1 lib/merb/mixins/erubis_capture.rb
merb-0.5.2 lib/merb/mixins/erubis_capture.rb
merb-0.5.3 lib/merb/mixins/erubis_capture.rb