Module: Hanami::Layout

Defined in:
lib/hanami/layout.rb

Overview

Layout

See Also:

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Register a layout

Examples:

require 'hanami/view'

class ApplicationLayout
  include Hanami::Layout
end

Since:

  • 0.1.0



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hanami/layout.rb', line 25

def self.included(base)
  conf = Hanami::View::Configuration.for(base)
  conf.add_layout(base)

  base.class_eval do
    extend Hanami::View::Dsl.dup
    extend ClassMethods

    include Utils::ClassAttribute
    class_attribute :configuration

    self.configuration = conf.duplicate
  end

  conf.copy!(base)
end

Instance Method Details

#initialize(scope, rendered) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a layout

Parameters:

  • scope (Hanami::View::Rendering::Scope, ::Hash)

    view rendering scope. Optionally a scope can be expressed as a Ruby ‘::Hash`, but it MUST contain the `:format` key, to specify which template to render.

  • rendered (String)

    the output of the view rendering process

Options Hash (scope):

  • :format (Symbol)

    the format to render (e.g. ‘:html`, `:xml`, `:json`) This is mandatory only if a `:Hash` is passed as `scope`.

See Also:

Since:

  • 0.1.0



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/hanami/layout.rb', line 132

def initialize(scope, rendered)
  # NOTE: This complex data transformation is due to a combination of a bug and the intent of maintaing backward compat (SemVer).
  # See https://github.com/hanami/view/pull/156
  s, r = *case scope
          when ::Hash
            [Hanami::View::Rendering::Scope.new(Hanami::View::Rendering::NullView, scope), rendered]
          when Hanami::View::Template
            [Hanami::View::Rendering::Scope.new(Hanami::View::Rendering::NullView, rendered.merge(format: scope.format)), ""]
          else
            [scope, rendered]
          end

  @scope = View::Rendering::LayoutScope.new(self, s)
  @rendered = r
end

#local(key) ⇒ Object, Hanami::View::Rendering::NullLocal

It tries to invoke a method for the view or a local for the given key. If the lookup fails, it returns a null object.

Examples:

Safe method navigation

class ApplicationLayout
  include Hanami::Layout

  def render_flash
    return if local(:flash).nil?

    # ...
  end
end

Returns:

Since:

  • 1.1.0



177
178
179
# File 'lib/hanami/layout.rb', line 177

def local(key)
  @scope.local(key)
end

#renderString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Render the layout

Returns:

  • (String)

    the output of the rendering process

See Also:

Since:

  • 0.1.0



156
157
158
# File 'lib/hanami/layout.rb', line 156

def render
  template.render(@scope, &Proc.new{@rendered})
end