Sha256: 6603b009505483d7d4f1704cd9545b2ddc8673d694d97b5fb403608f7d597b4e

Contents?: true

Size: 1.7 KB

Versions: 11

Compression:

Stored size: 1.7 KB

Contents

class Roda
  module RodaPlugins
    # The content_for plugin is designed to be used with the
    # render plugin, allowing you to store content inside one
    # template, and retrieve that content inside a separate
    # template.  Most commonly, this is so view templates
    # can set content for the layout template to display outside
    # of the normal content pane.
    #
    # The content_for template probably only works with erb
    # templates, and requires that you don't override the
    # +:outvar+ render option.  In the template in which you
    # want to store content, call content_for with a block:
    #
    #   <% content_for :foo do %>
    #     Some content here.
    #   <% end %>
    #
    # In the template in which you want to retrieve content,
    # call content_for without the block:
    #
    #   <%= content_for :foo %>
    module ContentFor
      # Depend on the render plugin, since this plugin only makes
      # sense when the render plugin is used.
      def self.load_dependencies(app)
        app.plugin :render
      end

      module InstanceMethods
        # If called with a block, store content enclosed by block
        # under the given key.  If called without a block, retrieve
        # stored content with the given key, or return nil if there
        # is no content stored with that key.
        def content_for(key, &block)
          if block
            @_content_for ||= {}
            buf_was = @_out_buf
            @_out_buf = ''
            yield
            @_content_for[key] = @_out_buf
            @_out_buf = buf_was
          elsif @_content_for
            @_content_for[key]
          end
        end
      end
    end

    register_plugin(:content_for, ContentFor)
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
roda-2.7.0 lib/roda/plugins/content_for.rb
roda-2.6.0 lib/roda/plugins/content_for.rb
roda-2.5.1 lib/roda/plugins/content_for.rb
roda-2.5.0 lib/roda/plugins/content_for.rb
roda-2.4.0 lib/roda/plugins/content_for.rb
roda-2.3.0 lib/roda/plugins/content_for.rb
roda-2.2.0 lib/roda/plugins/content_for.rb
roda-2.1.0 lib/roda/plugins/content_for.rb
roda-2.0.0 lib/roda/plugins/content_for.rb
roda-1.3.0 lib/roda/plugins/content_for.rb
roda-1.2.0 lib/roda/plugins/content_for.rb