Class: Unmagic::ComponentPartial::Partial

Inherits:
Object
  • Object
show all
Defined in:
lib/unmagic/component_partial/partial.rb

Overview

A handle yielded to the block of a partial rendered as a layout. The block fills named slots; the partial reads them back wherever it likes — for content that isn’t the main body, e.g. a card footer.

Instance Method Summary collapse

Constructor Details

#initialize(view_context) ⇒ Partial

Returns a new instance of Partial.



12
13
14
15
# File 'lib/unmagic/component_partial/partial.rb', line 12

def initialize(view_context)
  @view_context = view_context
  @contents = Hash.new { |h, k| h[k] = ActiveSupport::SafeBuffer.new }
end

Instance Method Details

#content_for(name, content = nil, &block) ⇒ Object

Write a slot (string or block), or read it back when called without content. Reading an unset slot returns nil.



19
20
21
22
23
24
25
26
27
# File 'lib/unmagic/component_partial/partial.rb', line 19

def content_for(name, content = nil, &block)
  if content || block
    content = @view_context.capture(&block) if block
    @contents[name] << content.to_s
    nil
  else
    @contents[name].presence
  end
end