Sha256: 235282f88b713c5be83f3e4ab59727e5b06d95086fffc353f6a3f3782cc045cf

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module Sinatra
  module ContentFor
    # Capture a block of content to be rendered later. For example:
    #
    #     <% content_for :head do %>
    #       <script type="text/javascript" src="/foo.js"></script>
    #     <% end %>
    #
    # You can call +content_for+ multiple times with the same key
    # (in the example +:head+), and when you render the blocks for
    # that key all of them will be rendered, in the same order you
    # captured them.
    def content_for(key, &block)
      content_blocks[key.to_sym] << begin
        if respond_to?(:block_is_haml?) && block_is_haml?(block)
          capture_haml(&block)
        else
          block.call
        end
      end
    end

    # Render the captured blocks for a given key. For example:
    #
    #     <head>
    #       <title>Example</title>
    #       <% yield_content :head %>
    #     </head>
    #
    # Would render everything you declared with <tt>content_for 
    # :head</tt> before closing the <tt><head></tt> tag.
    def yield_content(key)
      content_blocks[key.to_sym].join
    end

    private

      def content_blocks
        @content_blocks ||= Hash.new {|h,k| h[k] = [] }
      end
  end

  helpers ContentFor
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
toolmantim-sinatra-content-for-0.2.1 lib/sinatra/content_for.rb