Module: Roda::RodaPlugins::ContentFor::InstanceMethods

Defined in:
lib/roda/plugins/content_for.rb

Instance Method Summary collapse

Instance Method Details

#content_for(key, value = nil, &block) ⇒ Object

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.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/roda/plugins/content_for.rb', line 55

def content_for(key, value=nil, &block)
  append = opts[:append_content_for]

  if block || value
    if block
      outvar = render_opts[:template_opts][:outvar]
      buf_was = instance_variable_get(outvar)

      # Use temporary output buffer for ERB-based rendering systems
      instance_variable_set(outvar, String.new)
      value = Tilt[render_opts[:engine]].new(&block).render
      instance_variable_set(outvar, buf_was)
    end

    @_content_for ||= {}

    if append
      (@_content_for[key] ||= []) << value
    else
      if @_content_for[key] && append.nil?
        RodaPlugins.warn "Attempt to set content_for with same key.  This currently overwrites the existing content_for for #{key}.  In Roda 3, it will start appending to the existing content_for by default.  Use the :append => false option to the content_for plugin to keep the existing behavior."
      end
      @_content_for[key] = value
    end
  elsif @_content_for && (value = @_content_for[key])
    if append
      value = value.join
    end

    value
  end
end