Class: Dry::View::Layout
- Inherits:
-
Object
- Object
- Dry::View::Layout
- Extended by:
- Configurable
- Defined in:
- lib/dry/view/layout.rb
Constant Summary collapse
- DEFAULT_DIR =
'layouts'.freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#default_format ⇒ Object
readonly
Returns the value of attribute default_format.
-
#layout_dir ⇒ Object
readonly
Returns the value of attribute layout_dir.
-
#layout_path ⇒ Object
readonly
Returns the value of attribute layout_path.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#template_path ⇒ Object
readonly
Returns the value of attribute template_path.
Class Method Summary collapse
Instance Method Summary collapse
- #call(options = {}) ⇒ Object
-
#initialize ⇒ Layout
constructor
A new instance of Layout.
- #locals(options) ⇒ Object
- #parts(locals, renderer) ⇒ Object
Constructor Details
#initialize ⇒ Layout
Returns a new instance of Layout.
48 49 50 51 52 53 54 55 |
# File 'lib/dry/view/layout.rb', line 48 def initialize @config = self.class.config @default_format = self.class.default_format @layout_dir = DEFAULT_DIR @layout_path = "#{layout_dir}/#{config.name}" @template_path = config.template @scope = config.scope end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
25 26 27 |
# File 'lib/dry/view/layout.rb', line 25 def config @config end |
#default_format ⇒ Object (readonly)
Returns the value of attribute default_format.
25 26 27 |
# File 'lib/dry/view/layout.rb', line 25 def default_format @default_format end |
#layout_dir ⇒ Object (readonly)
Returns the value of attribute layout_dir.
25 26 27 |
# File 'lib/dry/view/layout.rb', line 25 def layout_dir @layout_dir end |
#layout_path ⇒ Object (readonly)
Returns the value of attribute layout_path.
25 26 27 |
# File 'lib/dry/view/layout.rb', line 25 def layout_path @layout_path end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
25 26 27 |
# File 'lib/dry/view/layout.rb', line 25 def scope @scope end |
#template_path ⇒ Object (readonly)
Returns the value of attribute template_path.
25 26 27 |
# File 'lib/dry/view/layout.rb', line 25 def template_path @template_path end |
Class Method Details
.default_format ⇒ Object
44 45 46 |
# File 'lib/dry/view/layout.rb', line 44 def self.default_format config.formats.keys.first end |
.renderer(format = default_format) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/dry/view/layout.rb', line 28 def self.renderer(format = default_format) unless config.formats.key?(format.to_sym) raise ArgumentError, "format +#{format}+ is not configured" end renderers[format] end |
Instance Method Details
#call(options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/dry/view/layout.rb', line 57 def call( = {}) renderer = self.class.renderer(.fetch(:format, default_format)) template_content = renderer.(template_path, template_scope(, renderer)) renderer.(layout_path, layout_scope(, renderer)) do template_content end end |
#locals(options) ⇒ Object
67 68 69 |
# File 'lib/dry/view/layout.rb', line 67 def locals() .fetch(:locals, {}) end |
#parts(locals, renderer) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/dry/view/layout.rb', line 71 def parts(locals, renderer) return empty_part(template_path, renderer) unless locals.any? part_hash = locals.each_with_object({}) do |(key, value), result| part = case value when Array el_key = Inflecto.singularize(key).to_sym template_part( key, renderer, value.map { |element| template_part(el_key, renderer, element) } ) else template_part(key, renderer, value) end result[key] = part end part(template_path, renderer, part_hash) end |