Module: Hanami::View::Dsl

Defined in:
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



77
78
79
80
81
82
83
# File 'lib/hanami/view/dsl.rb', line 77

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



315
316
317
318
319
320
321
322
323
324
# File 'lib/hanami/view/dsl.rb', line 315

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



45
46
47
48
49
50
51
# File 'lib/hanami/view/dsl.rb', line 45

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



206
207
208
209
210
211
212
# File 'lib/hanami/view/dsl.rb', line 206

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