Module: WrapIt::Sections

Extended by:
DerivedAttributes, ClassMethods
Included in:
Base
Defined in:
lib/wrap_it/sections.rb

Overview

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.

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.

Examples:

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:

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

#place(src, to) ⇒ void #place(src, at, dst) ⇒ void Originally defined in module ClassMethods

This method returns an undefined value.

Places specific section in specified place

Overloads:

  • #place(src, to) ⇒ void

    Parameters:

    • src (Symbol)

      section name to place

    • to (Hash)

      single key-value hash. Key can be :before or after, value can be :begin, :end or section name

  • #place(src, at, dst) ⇒ void

    Parameters:

    • src (Symbol)

      section name to place

    • at (Symbol)

      can be :before or :after

    • dst (Symbol)

      can be :begin, :end or section name

.placementArray<Symbol> Originally defined in module ClassMethods

Retrieves section names in current order

Returns:

  • (Array<Symbol>)

    ordered sections array

#section([name, ...]) ⇒ void Originally defined in module ClassMethods

This method returns an undefined value.

Defines new section or sections. Places its to end of section list

Parameters:

  • name (Symbol, String)

    section name

.sectionsArray<Symbol> Originally defined in module ClassMethods

Retrieves all sections, including ancestors

Returns:

  • (Array<Symbol>)

    array of sections

Instance Method Details

#[](name) ⇒ String

Retrieves specified section content

Parameters:

  • name (Symbol)

    section name

Returns:

  • (String)

    section content



71
72
73
74
75
76
# 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) ⇒ String

Sets specified section content

Parameters:

  • name (Symbol)

    section name

  • value (String)

    content

Returns:

  • (String)

    section content



84
85
86
87
88
89
# 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