Module: Hanami::View::Dsl
- Defined in:
- lib/hanami/view/dsl.rb
Overview
Class level DSL
Instance Method Summary collapse
-
#format(value = nil) ⇒ Symbol?
When a value is given, specify the handled format.
-
#layout(value = nil) ⇒ Symbol?
When a value is given, it specifies the layout.
-
#root(value = nil) ⇒ Pathname
When a value is given, specify a templates root path for the view.
-
#template(value = nil) ⇒ String
When a value is given, specify the relative path to the template.
Instance Method Details
#format(value = nil) ⇒ Symbol?
When a value is given, specify the handled format. Otherwise, it returns the previously specified format.
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.
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`.
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'
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 |