Module: Hanami::View::Dsl

Defined in:
gems/gems/hanami-view-1.3.0/lib/hanami/view/dsl.rb

Overview

Class level DSL

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#format(value = nil) ⇒ Symbol?

When a value is given, specify the handled format. Otherwise, it returns the previously specified format.

Examples:

require 'hanami/view'

module Articles
  class Show
    include Hanami::View
  end

  class JsonShow < Show
    format :json
  end
end

Articles::Show.format     # => nil
Articles::JsonShow.format # => :json

Parameters:

  • value (Symbol) (defaults to: nil)

    the format

Returns:

  • (Symbol, nil)

    the specified format for this view, if set

Since:

  • 0.1.0

def format(value = nil)
  if value.nil?
    @format ||= nil
  else
    @format = value
  end
end

#layout(value = nil) ⇒ Symbol?

When a value is given, it specifies the layout. When false is given, Hanami::View::Rendering::NullLayout is returned. Otherwise, it returns the previously specified layout.

When the global configuration is set (Hanami::View.layout=), after the loading process, it will return that layout if not otherwise specified.

Examples:

Default usage

require 'hanami/view'

module Articles
  class Show
    include Hanami::View
  end
end

Articles::Show.layout # => nil

Custom layout

require 'hanami/view'

class ArticlesLayout
  include Hanami::Layout
end

module Articles
  class Show
    include Hanami::View
    layout :articles
  end
end

Articles::Show.layout # => :articles

Global configuration

require 'hanami/view'

class ApplicationLayout
  include Hanami::Layout
end

module Articles
  class Show
    include Hanami::View
  end
end

Hanami::View.layout = :application
Articles::Show.layout # => nil

Hanami::View.load!
Articles::Show.layout # => :application

Global configuration with custom layout

require 'hanami/view'

class ApplicationLayout
  include Hanami::Layout
end

class ArticlesLayout
  include Hanami::Layout
end

module Articles
  class Show
    include Hanami::View
    layout :articles
  end
end

Hanami::View.layout = :application
Articles::Show.layout # => :articles

Hanami::View.load!
Articles::Show.layout # => :articles

Disable layout for the view

require 'hanami/view'

class ApplicationLayout
  include Hanami::Layout
end

module Articles
  class Show
    include Hanami::View
    layout false
  end
end

Hanami::View.load!
Articles::Show.layout # => Hanami::View::Rendering::NullLayout

Parameters:

  • value (Symbol, FalseClass, nil) (defaults to: nil)

    the layout name

Returns:

  • (Symbol, nil)

    the specified layout for this view, if set

See Also:

Since:

  • 0.1.0

def layout(value = nil)
  if value.nil?
    @layout  ||= nil
    @_layout ||= Rendering::LayoutFinder.find(@layout || configuration.layout, configuration.namespace)
  elsif !value
    @layout = Hanami::View::Rendering::NullLayout
  else
    @layout = value
  end
end

#root(value = nil) ⇒ Pathname

When a value is given, specify a templates root path for the view. Otherwise, it returns templates root path.

When not initialized, it will return the global value from Hanami::View.root.

Examples:

Default usage

require 'hanami/view'

module Articles
  class Show
    include Hanami::View
  end
end

Hanami::View.configuration.root # => 'app/templates'
Articles::Show.root            # => 'app/templates'

Custom root

require 'hanami/view'

module Articles
  class Show
    include Hanami::View
    root 'path/to/articles/templates'
  end
end

Hanami::View.configuration.root # => 'app/templates'
Articles::Show.root            # => 'path/to/articles/templates'

Parameters:

  • value (String) (defaults to: nil)

    the templates root for this view

Returns:

  • (Pathname)

    the specified root for this view or the global value

Since:

  • 0.1.0

def root(value = nil)
  if value.nil?
    configuration.root
  else
    configuration.root(value)
  end
end

#template(value = nil) ⇒ String

When a value is given, specify the relative path to the template. Otherwise, it returns the name that follows Hanami::View conventions.

@example Default usage require 'hanami/view'

module Articles class Show include Hanami::View end

class JsonShow < Show
  format :json
end

end

Articles::Show.template # => 'articles/show' Articles::JsonShow.template # => 'articles/show'

@example Custom template require 'hanami/view'

module Articles class Show include Hanami::View template 'articles/single_article' end

class JsonShow < Show
  format :json
end

end

Articles::Show.template # => 'articles/single_article' Articles::JsonShow.template # => 'articles/single_article'

Examples:

With namespace

require 'hanami/view'

module Furnitures
  View = Hanami::View.generate(self)

  class Standalone
    include Furnitures::View
  end

  module Catalog
    class Index
      Furnitures::View
    end
  end
end

Furnitures::Standalone.template     # => 'standalone'
Furnitures::Catalog::Index.template # => 'catalog/index'

With nested namespace

require 'hanami/view'

module Frontend
  View = Hanami::View.generate(self)

  class StandaloneView
    include Frontend::View
  end

  module Views
    class Standalone
      include Frontend::View
    end

    module Sessions
      class New
        include Frontend::View
      end
    end
  end
end

Frontend::StandaloneView.template       # => 'standalone_view'
Frontend::Views::Standalone.template    # => 'standalone'
Frontend::Views::Sessions::New.template # => 'sessions/new'

With deeply nested namespace

require 'hanami/view'

module Bookshelf
  module Web
    View = Hanami::View.generate(self)

    module Views
      module Books
        class Show
          include Bookshelf::Web::View
        end
      end
    end
  end

  module Api
    View = Hanami::View.generate(self)

    module Views
      module Books
        class Show
          include Bookshelf::Api::View
        end
      end
    end
  end
end

Bookshelf::Web::Views::Books::Index.template # => 'books/index'
Bookshelf::Api::Views::Books::Index.template # => 'books/index'

Parameters:

  • value (String) (defaults to: nil)

    relative template path

Returns:

  • (String)

    the specified template for this view or the name that follows the convention

Since:

  • 0.1.0

def template(value = nil)
  if value.nil?
    @template ||= Rendering::TemplateName.new(name, configuration.namespace).to_s
  else
    @template = value
  end
end