Module: Lotus::View::Dsl

Defined in:
lib/lotus/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 'lotus/view'

module Articles
  class Show
    include Lotus::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/lotus/view/dsl.rb', line 77

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

#layout(value = nil) ⇒ Symbol?

When a value is given, it specifies the layout. Otherwise, it returns the previously specified layout.

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

Examples:

Default usage

require 'lotus/view'

module Articles
  class Show
    include Lotus::View
  end
end

Articles::Show.layout # => nil

Custom layout

require 'lotus/view'

class ArticlesLayout
  include Lotus::Layout
end

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

Articles::Show.layout # => :articles

Global configuration

require 'lotus/view'

class ApplicationLayout
  include Lotus::Layout
end

module Articles
  class Show
    include Lotus::View
  end
end

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

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

Global configuration with custom layout

require 'lotus/view'

class ApplicationLayout
  include Lotus::Layout
end

class ArticlesLayout
  include Lotus::Layout
end

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

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

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

Parameters:

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

    the layout name

Returns:

  • (Symbol, nil)

    the specified layout for this view, if set

See Also:

Since:

  • 0.1.0



218
219
220
221
222
223
224
# File 'lib/lotus/view/dsl.rb', line 218

def layout(value = nil)
  if value
    @layout = value
  else
    @layout
  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 ‘Lotus::View.root`.

Examples:

Default usage

require 'lotus/view'

module Articles
  class Show
    include Lotus::View
  end
end

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

Custom root

require 'lotus/view'

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

Lotus::View.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/lotus/view/dsl.rb', line 45

def root(value = nil)
  if value
    @@root = Pathname.new value
  else
    @@root ||= Lotus::View.root
  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 Lotus::View conventions.

 @example Default usage

require 'lotus/view'

module Articles
  class Show
    include Lotus::View
  end

  class JsonShow < Show
    format :json
  end
end

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

 @example Custom template

require 'lotus/view'

module Articles
  class Show
    include Lotus::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'

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



127
128
129
130
131
132
133
# File 'lib/lotus/view/dsl.rb', line 127

def template(value = nil)
  if value
    @@template = value
  else
    @@template ||= Utils::String.new(name).underscore
  end
end