Class: Unmagic::ComponentPartial::Partial
- Inherits:
-
Object
- Object
- Unmagic::ComponentPartial::Partial
- 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. A slot can also carry props: structured data the partial reads back with #props_for.
Instance Method Summary collapse
-
#content_for(name, content = nil, **props, &block) ⇒ Object
Write a slot (string or block, with optional props), or read it back when called with nothing.
-
#initialize(view_context) ⇒ Partial
constructor
A new instance of Partial.
-
#props_for(name) ⇒ Object
Read the props attached to a slot, or an empty hash when none were set.
Constructor Details
#initialize(view_context) ⇒ Partial
Returns a new instance of Partial.
13 14 15 16 17 |
# File 'lib/unmagic/component_partial/partial.rb', line 13 def initialize(view_context) @view_context = view_context @contents = Hash.new { |h, k| h[k] = ActiveSupport::SafeBuffer.new } @props = {} end |
Instance Method Details
#content_for(name, content = nil, **props, &block) ⇒ Object
Write a slot (string or block, with optional props), or read it back when called with nothing. Reading an unset slot returns nil.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/unmagic/component_partial/partial.rb', line 21 def content_for(name, content = nil, **props, &block) if content || block || props.any? content = @view_context.capture(&block) if block @contents[name] << content.to_s (@props[name] ||= {}).merge!(props) if props.any? nil else @contents[name].presence end end |
#props_for(name) ⇒ Object
Read the props attached to a slot, or an empty hash when none were set.
33 34 35 |
# File 'lib/unmagic/component_partial/partial.rb', line 33 def props_for(name) @props[name] || {} end |