Class: Hanami::View::Configuration

Inherits:
Object
  • Object
show all
Defined in:
gems/gems/hanami-view-1.2.0/lib/hanami/view/configuration.rb,
gems/gems/hanami-view-1.3.3/lib/hanami/view/configuration.rb

Overview

Configuration for the framework, controllers and actions.

Hanami::Controller has its own global configuration that can be manipulated via Hanami::View.configure.

Every time that Hanami::View and Hanami::Layout are included, that global configuration is being copied to the recipient. The copy will inherit all the settings from the original, but all the subsequent changes aren't reflected from the parent to the children, and viceversa.

This architecture allows to have a global configuration that capture the most common cases for an application, and let views and layouts layouts to specify exceptions.

Since:

  • 0.2.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHanami::View::Configuration

Initialize a configuration instance

Since:

  • 0.2.0

def initialize
  @namespace = Object
  reset!
end

Instance Attribute Details

#default_encoding(value) ⇒ Object #default_encodingEncoding

Default encoding for templates

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Set UTF-8 As A String

require 'hanami/view'

Hanami::View.configure do
  default_encoding 'utf-8'
end

Set UTF-8 As An Encoding Constant

require 'hanami/view'

Hanami::View.configure do
  default_encoding Encoding::UTF_8
end

Raise An Error For Unknown Encoding

require 'hanami/view'

Hanami::View.configure do
  default_encoding 'foo'
end

  # => ArgumentError

Overloads:

  • #default_encoding(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (String, Encoding)

      a string representation of the encoding, or an Encoding constant

    Raises:

    • (ArgumentError)

      if the given value isn't a supported encoding

  • #default_encodingEncoding

    Gets the value

    Returns:

    • (Encoding)

Since:

  • 0.5.0

def default_encoding(value = nil)
  if value.nil?
    @default_encoding
  else
    @default_encoding = Encoding.find(value)
  end
end

#layout(value) ⇒ Object #layoutClass

Set the global layout

If not set, this value defaults to nil, while at the rendering time it will use Hanami::View::Rendering::NullLayout.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Getting the value

require 'hanami/view'

Hanami::View.configuration.layout # => nil

Setting the value

require 'hanami/view'

Hanami::View.configure do
  layout :application
end

Hanami::View.configuration.layout # => ApplicationLayout

Setting the value in a namespaced app

require 'hanami/view'

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

MyApp::View.configuration.layout # => MyApp::ApplicationLayout

Overloads:

  • #layout(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (Symbol)

      the name of the layout

  • #layoutClass

    Gets the value

    Returns:

    • (Class)

See Also:

Since:

  • 0.2.0

def layout(value = nil)
  if value.nil?
    Rendering::LayoutFinder.find(@layout, @namespace)
  else
    @layout = value
  end
end

#layoutsObject (readonly)

Since:

  • 0.2.0

def layouts
  @layouts
end

#load_pathsObject

Since:

  • 0.2.0

def load_paths
  @load_paths
end

#modulesObject

Since:

  • 0.2.0

def modules
  @modules
end

#namespace(value) ⇒ Object #namespaceClass, ...

Set the Ruby namespace where to lookup for views.

When multiple instances of the framework are used, we want to make sure that if a MyApp wants a Dashboard::Index view, we are loading the right one.

If not set, this value defaults to Object.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Getting the value

require 'hanami/view'

Hanami::View.configuration.namespace # => Object

Setting the value

require 'hanami/view'

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

Overloads:

  • #namespace(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (Class, Module, String)

      a valid Ruby namespace identifier

  • #namespaceClass, ...

    Gets the value

    Returns:

    • (Class, Module, String)

Since:

  • 0.2.0

def namespace(value = nil)
  if value
    @namespace = value
  else
    @namespace
  end
end

#partialsObject (readonly)

Since:

  • 0.2.0

def partials
  @partials
end

#root(value) ⇒ Object #rootPathname

Set the root path where to search for templates

If not set, this value defaults to the current directory.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Getting the value

require 'hanami/view'

Hanami::View.configuration.root # => #<Pathname:.>

Setting the value

require 'hanami/view'

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

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

Overloads:

  • #root(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (String, Pathname, #to_pathname)

      an object that can be coerced to Pathname

    Raises:

    • (Errno::ENOENT)

      if the given path doesn't exist

  • #rootPathname

    Gets the value

    Returns:

    • (Pathname)

See Also:

Since:

  • 0.2.0

def root(value = nil)
  if value
    @root = Utils::Kernel.Pathname(value).realpath
  else
    @root
  end
end

#viewsObject (readonly)

Since:

  • 0.2.0

def views
  @views
end

Instance Method Details

#prepare(&blk) ⇒ void

This method returns an undefined value.

Prepare the views.

The given block will be yielded when Hanami::View will be included by a view.

This method can be called multiple times.

Examples:

Including shared utilities

require 'hanami/view'

module UrlHelpers
  def comments_path
    '/'
  end
end

Hanami::View.configure do
  prepare do
    include UrlHelpers
  end
end

Hanami::View.load!

module Comments
  class New
    # The following include will cause UrlHelpers to be included too.
    # This makes `comments_path` available in the view context
    include Hanami::View

    def form
      %(<form action="#{ comments_path }" method="POST"></form>)
    end
  end
end

Preparing multiple times

require 'hanami/view'

Hanami::View.configure do
  prepare do
    include UrlHelpers
  end

  prepare do
    format :json
  end
end

Hanami::View.configure do
  prepare do
    include FormattingHelpers
  end
end

Hanami::View.load!

module Articles
  class Index
    # The following include will cause the inclusion of:
    #   * UrlHelpers
    #   * FormattingHelpers
    #
    # It also sets the view to render only JSON
    include Hanami::View
  end
end

Parameters:

  • blk (Proc)

    the code block

Raises:

  • (ArgumentError)

    if called without passing a block

See Also:

Since:

  • 0.3.0

def prepare(&blk)
  if block_given?
    @modules.push(blk)
  else
    raise ArgumentError.new('Please provide a block')
  end
end