module WrapIt::Sections

Sections is a smart way to make complex components with inheritance.

Sections is just array of named HTML markup pieces. You can place any section before or after another at class level and change their content at instance level.

Each component have three stages. First is initialization, then sections capturing and rendering. You can change any sections content until rendering stage begins. Finally, renderer joins all sections in order, that they have at render time.

{WrapIt::Base} provides following sections: main section is `:content`. All text from block captured there. `:render_arguments` and `:render_block` also provided, so arguments and block passed to render method captured here.

Access to sections at instance level performed throw hash-like getter and setter ([] and []=) of self.

With this functionality you can easy organize you inheritance, so any descendant can change sections order or any section content without changes to unrelated sections.

@example sections usage

class IconedButton < WrapIt::Base
  include TextContainer
  html_class 'btn'
  section :icon
  place :icon, before: :content

  after_capture do
    self[:icon] = html_safe('<i class="my-icon"></i>')
  end
end

class RightIconedButton < IconedButton
  place :icon, after: :content
end

b1 = IconedButton.new(template, 'text')
b2 = RightIconedButton.new(template, 'text')
b1.render # => '<div class="btn"><i class="my-icon"></i>text</div>'
b2.render # => '<div class="btn">text<i class="my-icon"></i></div>'

@author Alexey Ovchinnikov <alexiss@cybernetlab.ru>

Public Class Methods

included(base) click to toggle source
# File lib/wrap_it/sections.rb, line 58
def self.included(base)
  base == Base || fail(
    TypeError,
    "#{self.class.name} can be included only into WrapIt::Base"
  )
  base.extend ClassMethods
end

Public Instance Methods

[](name) click to toggle source

Retrieves specified section content @param name [Symbol] section name

@return [String] section content

# File lib/wrap_it/sections.rb, line 71
def [](name)
  @section_names ||= self.class.sections
  return nil unless @section_names.include?(name)
  @sections ||= {}
  @sections[name] ||= empty_html
end
[]=(name, value) click to toggle source

Sets specified section content @param name [Symbol] section name @param value [String] content

@return [String] section content

# File lib/wrap_it/sections.rb, line 84
def []=(name, value)
  @section_names ||= self.class.sections
  return unless @section_names.include?(name)
  @sections ||= {}
  @sections[name] = value
end