Module: Hanami::View

Includes:
Utils::ClassAttribute
Defined in:
lib/hanami/view.rb,
lib/hanami/view/dsl.rb,
lib/hanami/view/errors.rb,
lib/hanami/view/escape.rb,
lib/hanami/view/version.rb,
lib/hanami/view/template.rb,
lib/hanami/view/rendering.rb,
lib/hanami/view/inheritable.rb,
lib/hanami/view/configuration.rb,
lib/hanami/view/rendering/scope.rb,
lib/hanami/view/rendering/options.rb,
lib/hanami/view/rendering/partial.rb,
lib/hanami/view/rendering/registry.rb,
lib/hanami/view/rendering/subscope.rb,
lib/hanami/view/rendering/template.rb,
lib/hanami/view/rendering/null_view.rb,
lib/hanami/view/rendering/null_local.rb,
lib/hanami/view/rendering/null_layout.rb,
lib/hanami/view/rendering/view_finder.rb,
lib/hanami/view/rendering/layout_scope.rb,
lib/hanami/view/rendering/partial_file.rb,
lib/hanami/view/rendering/layout_finder.rb,
lib/hanami/view/rendering/null_template.rb,
lib/hanami/view/rendering/template_name.rb,
lib/hanami/view/rendering/partial_finder.rb,
lib/hanami/view/rendering/layout_registry.rb,
lib/hanami/view/rendering/template_finder.rb,
lib/hanami/view/rendering/templates_finder.rb,
lib/hanami/view/rendering/partial_templates_finder.rb

Overview

View

Since:

  • 0.1.0

Defined Under Namespace

Modules: Dsl, Escape, Inheritable, Rendering Classes: Configuration, Error, MissingFormatError, MissingTemplateError, MissingTemplateLayoutError, Template, UnknownRenderTypeError

Constant Summary collapse

VERSION =

Defines the version

Since:

  • 0.1.0

'1.3.3'.freeze

Class Method Summary collapse

Class Method Details

.configure(&blk) ⇒ Object

Configure the framework. It yields the given block in the context of the configuration

Examples:

require 'hanami/view'

Hanami::View.configure do
  root '/path/to/root'
end

Parameters:

  • blk (Proc)

    the configuration block

See Also:

Since:

  • 0.2.0


45
46
47
# File 'lib/hanami/view.rb', line 45

def self.configure(&blk)
  configuration.instance_eval(&blk)
end

.dupeModule

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.

Duplicate Hanami::View in order to create a new separated instance of the framework.

The new instance of the framework will be completely decoupled from the original. It will inherit the configuration, but all the changes that happen after the duplication, won't be reflected on the other copies.

Examples:

Basic usage

require 'hanami/view'

module MyApp
  View = Hanami::View.dupe
end

MyApp::View == Hanami::View # => false

MyApp::View.configuration ==
  Hanami::View.configuration # => false

Inheriting configuration

require 'hanami/view'

Hanami::View.configure do
  root '/path/to/root'
end

module MyApp
  View = Hanami::View.dupe
end

module MyApi
  View = Hanami::View.dupe
  View.configure do
    root '/another/root'
  end
end

Hanami::View.configuration.root # => #<Pathname:/path/to/root>
MyApp::View.configuration.root # => #<Pathname:/path/to/root>
MyApi::View.configuration.root # => #<Pathname:/another/root>

Returns:

  • (Module)

    a copy of Hanami::View

Since:

  • 0.2.0


94
95
96
97
98
# File 'lib/hanami/view.rb', line 94

def self.dupe
  dup.tap do |duplicated|
    duplicated.configuration = configuration.duplicate
  end
end

.duplicate(mod, views = 'Views', &blk) ⇒ Module

Duplicate the framework and generate modules for the target application

module MyApp::Views::Dashboard
  class Index
    include MyApp::View
  end
end

Examples:

Basic usage

require 'hanami/view'

module MyApp
  View = Hanami::View.duplicate(self)
end

# It will:
#
# 1. Generate MyApp::View
# 2. Generate MyApp::Layout
# 3. Generate MyApp::Presenter
# 4. Generate MyApp::Views
# 5. Configure MyApp::Views as the default namespace for views

Compare code

require 'hanami/view'

module MyApp
  View = Hanami::View.duplicate(self) do
    # ...
  end
end

# it's equivalent to:

module MyApp
  View   = Hanami::View.dupe
  Layout = Hanami::Layout.dup

  module Views
  end

  View.configure do
    namespace 'MyApp::Views'
  end

  View.configure do
    # ...
  end
end

Custom views module

require 'hanami/view

module MyApp
  View = Hanami::View.duplicate(self, 'Vs')
end

defined?(MyApp::Views) # => nil
defined?(MyApp::Vs)    # => "constant"

# Developers can namespace views under Vs
module MyApp::Vs::Dashboard
  # ...
end

Nil views module

require 'hanami/view'

module MyApp
  View = Hanami::View.duplicate(self, nil)
end

defined?(MyApp::Views) # => nil

# Developers can namespace views under MyApp
module MyApp
  # ...
end

Block usage

require 'hanami/view'

module MyApp
  View = Hanami::View.duplicate(self) do
    root '/path/to/root'
  end
end

Hanami::View.configuration.root # => #<Pathname:.>
MyApp::View.configuration.root # => #<Pathname:/path/to/root>

Parameters:

  • mod (Module)

    the Ruby namespace of the application

  • views (String) (defaults to: 'Views')

    the optional namespace where the application's views will live

  • blk (Proc)

    an optional block to configure the framework

Returns:

  • (Module)

    a copy of Hanami::View

See Also:

Since:

  • 0.2.0


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/hanami/view.rb', line 203

def self.duplicate(mod, views = 'Views', &blk)
  dupe.tap do |duplicated|
    mod.module_eval %{ module #{ views }; end } if views
    mod.module_eval %{
      Layout = Hanami::Layout.dup
      Presenter = Hanami::Presenter.dup
    }

    duplicated.configure do
      namespace [mod, views].compact.join '::'
    end

    duplicated.configure(&blk) if block_given?
  end
end

.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.

Override Ruby's hook for modules. It includes basic Hanami::View modules to the given Class. It sets a copy of the framework configuration

Examples:

require 'hanami/view'

class IndexView
  include Hanami::View
end

Parameters:

  • base (Class)

    the target view

See Also:

Since:

  • 0.1.0


240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/hanami/view.rb', line 240

def self.included(base)
  conf = self.configuration
  conf.add_view(base)

  base.class_eval do
    extend Inheritable
    extend Dsl
    extend Rendering
    extend Escape

    include Utils::ClassAttribute
    class_attribute :configuration

    self.configuration = conf.duplicate
  end

  conf.copy!(base)
end

.load!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.

Load the framework

Since:

  • 0.1.0


263
264
265
# File 'lib/hanami/view.rb', line 263

def self.load!
  configuration.load!
end