hanami-view 1.3.3 → 2.0.0.alpha2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -10
- data/LICENSE +20 -0
- data/README.md +20 -862
- data/hanami-view.gemspec +26 -16
- data/lib/hanami-view.rb +3 -1
- data/lib/hanami/view.rb +208 -223
- data/lib/hanami/view/application_configuration.rb +77 -0
- data/lib/hanami/view/application_context.rb +35 -0
- data/lib/hanami/view/application_view.rb +89 -0
- data/lib/hanami/view/context.rb +97 -0
- data/lib/hanami/view/context_helpers/content_helpers.rb +26 -0
- data/lib/hanami/view/decorated_attributes.rb +82 -0
- data/lib/hanami/view/errors.rb +19 -56
- data/lib/hanami/view/exposure.rb +126 -0
- data/lib/hanami/view/exposures.rb +74 -0
- data/lib/hanami/view/part.rb +217 -0
- data/lib/hanami/view/part_builder.rb +140 -0
- data/lib/hanami/view/path.rb +68 -0
- data/lib/hanami/view/render_environment.rb +62 -0
- data/lib/hanami/view/render_environment_missing.rb +44 -0
- data/lib/hanami/view/rendered.rb +55 -0
- data/lib/hanami/view/renderer.rb +79 -0
- data/lib/hanami/view/scope.rb +189 -0
- data/lib/hanami/view/scope_builder.rb +98 -0
- data/lib/hanami/view/standalone_view.rb +396 -0
- data/lib/hanami/view/tilt.rb +78 -0
- data/lib/hanami/view/tilt/erb.rb +26 -0
- data/lib/hanami/view/tilt/erbse.rb +21 -0
- data/lib/hanami/view/tilt/haml.rb +26 -0
- data/lib/hanami/view/version.rb +5 -5
- metadata +114 -70
- data/LICENSE.md +0 -22
- data/lib/hanami/layout.rb +0 -190
- data/lib/hanami/presenter.rb +0 -98
- data/lib/hanami/view/configuration.rb +0 -504
- data/lib/hanami/view/dsl.rb +0 -347
- data/lib/hanami/view/escape.rb +0 -225
- data/lib/hanami/view/inheritable.rb +0 -54
- data/lib/hanami/view/rendering.rb +0 -294
- data/lib/hanami/view/rendering/layout_finder.rb +0 -128
- data/lib/hanami/view/rendering/layout_registry.rb +0 -69
- data/lib/hanami/view/rendering/layout_scope.rb +0 -281
- data/lib/hanami/view/rendering/null_layout.rb +0 -52
- data/lib/hanami/view/rendering/null_local.rb +0 -82
- data/lib/hanami/view/rendering/null_template.rb +0 -83
- data/lib/hanami/view/rendering/null_view.rb +0 -26
- data/lib/hanami/view/rendering/options.rb +0 -24
- data/lib/hanami/view/rendering/partial.rb +0 -31
- data/lib/hanami/view/rendering/partial_file.rb +0 -29
- data/lib/hanami/view/rendering/partial_finder.rb +0 -75
- data/lib/hanami/view/rendering/partial_templates_finder.rb +0 -73
- data/lib/hanami/view/rendering/registry.rb +0 -134
- data/lib/hanami/view/rendering/scope.rb +0 -108
- data/lib/hanami/view/rendering/subscope.rb +0 -56
- data/lib/hanami/view/rendering/template.rb +0 -69
- data/lib/hanami/view/rendering/template_finder.rb +0 -55
- data/lib/hanami/view/rendering/template_name.rb +0 -50
- data/lib/hanami/view/rendering/templates_finder.rb +0 -144
- data/lib/hanami/view/rendering/view_finder.rb +0 -37
- data/lib/hanami/view/template.rb +0 -57
data/lib/hanami/layout.rb
DELETED
@@ -1,190 +0,0 @@
|
|
1
|
-
require 'hanami/utils/class_attribute'
|
2
|
-
require 'hanami/view/rendering/layout_registry'
|
3
|
-
require 'hanami/view/rendering/layout_scope'
|
4
|
-
require 'hanami/view/rendering/null_layout'
|
5
|
-
require 'hanami/view/rendering/null_view'
|
6
|
-
|
7
|
-
module Hanami
|
8
|
-
# Layout
|
9
|
-
#
|
10
|
-
# @since 0.1.0
|
11
|
-
#
|
12
|
-
# @see Hanami::Layout::ClassMethods
|
13
|
-
module Layout
|
14
|
-
# Register a layout
|
15
|
-
#
|
16
|
-
# @api private
|
17
|
-
# @since 0.1.0
|
18
|
-
#
|
19
|
-
# @example
|
20
|
-
# require 'hanami/view'
|
21
|
-
#
|
22
|
-
# class ApplicationLayout
|
23
|
-
# include Hanami::Layout
|
24
|
-
# end
|
25
|
-
def self.included(base)
|
26
|
-
conf = Hanami::View::Configuration.for(base)
|
27
|
-
conf.add_layout(base)
|
28
|
-
|
29
|
-
base.class_eval do
|
30
|
-
extend Hanami::View::Dsl.dup
|
31
|
-
extend ClassMethods
|
32
|
-
|
33
|
-
include Utils::ClassAttribute
|
34
|
-
class_attribute :configuration
|
35
|
-
|
36
|
-
self.configuration = conf.duplicate
|
37
|
-
end
|
38
|
-
|
39
|
-
conf.copy!(base)
|
40
|
-
end
|
41
|
-
|
42
|
-
# Class level API
|
43
|
-
#
|
44
|
-
# @since 0.1.0
|
45
|
-
module ClassMethods
|
46
|
-
# Template name suffix
|
47
|
-
#
|
48
|
-
# @api private
|
49
|
-
# @since 0.1.0
|
50
|
-
#
|
51
|
-
# @see Hanami::Layout::ClassMethods#suffix
|
52
|
-
# @see Hanami::Layout::ClassMethods#template
|
53
|
-
SUFFIX = '_layout'.freeze
|
54
|
-
|
55
|
-
# A registry that holds all the registered layouts.
|
56
|
-
#
|
57
|
-
# @api private
|
58
|
-
# @since 0.1.0
|
59
|
-
#
|
60
|
-
# @see Hanami::View::Rendering::LayoutRegistry
|
61
|
-
def registry
|
62
|
-
@registry ||= View::Rendering::LayoutRegistry.new(self)
|
63
|
-
end
|
64
|
-
|
65
|
-
# Template name
|
66
|
-
#
|
67
|
-
# @api private
|
68
|
-
# @since 0.1.0
|
69
|
-
#
|
70
|
-
# @see Hanami::Layout::ClassMethods#SUFFIX
|
71
|
-
# @see Hanami::Layout::ClassMethods#suffix
|
72
|
-
#
|
73
|
-
# @example
|
74
|
-
# # Given a template 'templates/application.html.erb'
|
75
|
-
#
|
76
|
-
# class ApplicationLayout
|
77
|
-
# include Hanami::Layout
|
78
|
-
# end
|
79
|
-
#
|
80
|
-
# ApplicationLayout.template # => 'application'
|
81
|
-
def template
|
82
|
-
super.sub(suffix, '')
|
83
|
-
end
|
84
|
-
|
85
|
-
# Template name suffix
|
86
|
-
#
|
87
|
-
# @api private
|
88
|
-
# @since 0.1.0
|
89
|
-
#
|
90
|
-
# @see Hanami::Layout::ClassMethods#SUFFIX
|
91
|
-
# @see Hanami::Layout::ClassMethods#template
|
92
|
-
def suffix
|
93
|
-
SUFFIX
|
94
|
-
end
|
95
|
-
|
96
|
-
protected
|
97
|
-
|
98
|
-
# Loading mechanism hook.
|
99
|
-
#
|
100
|
-
# @api private
|
101
|
-
# @since 0.1.0
|
102
|
-
#
|
103
|
-
# @see Hanami::View.load!
|
104
|
-
def load!
|
105
|
-
load_registry!
|
106
|
-
configuration.freeze
|
107
|
-
end
|
108
|
-
|
109
|
-
private
|
110
|
-
|
111
|
-
# @api private
|
112
|
-
def load_registry!
|
113
|
-
@registry = nil
|
114
|
-
registry.freeze
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
# Initialize a layout
|
119
|
-
#
|
120
|
-
# @param scope [Hanami::View::Rendering::Scope,::Hash] view rendering scope.
|
121
|
-
# Optionally a scope can be expressed as a Ruby `::Hash`, but it MUST contain
|
122
|
-
# the `:format` key, to specify which template to render.
|
123
|
-
# @option scope [Symbol] :format the format to render (e.g. `:html`, `:xml`, `:json`)
|
124
|
-
# This is mandatory only if a `:Hash` is passed as `scope`.
|
125
|
-
#
|
126
|
-
# @param rendered [String] the output of the view rendering process
|
127
|
-
#
|
128
|
-
# @api private
|
129
|
-
# @since 0.1.0
|
130
|
-
#
|
131
|
-
# @see Hanami::View::Rendering#render
|
132
|
-
def initialize(scope, rendered)
|
133
|
-
# NOTE: This complex data transformation is due to a combination of a bug and the intent of maintaing backward compat (SemVer).
|
134
|
-
# See https://github.com/hanami/view/pull/156
|
135
|
-
s, r = *case scope
|
136
|
-
when ::Hash
|
137
|
-
[Hanami::View::Rendering::Scope.new(Hanami::View::Rendering::NullView, scope), rendered]
|
138
|
-
when Hanami::View::Template
|
139
|
-
[Hanami::View::Rendering::Scope.new(Hanami::View::Rendering::NullView, rendered.merge(format: scope.format)), ""]
|
140
|
-
else
|
141
|
-
[scope, rendered]
|
142
|
-
end
|
143
|
-
|
144
|
-
@scope = View::Rendering::LayoutScope.new(self, s)
|
145
|
-
@rendered = r
|
146
|
-
end
|
147
|
-
|
148
|
-
# Render the layout
|
149
|
-
#
|
150
|
-
# @return [String] the output of the rendering process
|
151
|
-
#
|
152
|
-
# @api private
|
153
|
-
# @since 0.1.0
|
154
|
-
#
|
155
|
-
# @see Hanami::View::Rendering#render
|
156
|
-
def render
|
157
|
-
template.render(@scope, &Proc.new{@rendered})
|
158
|
-
end
|
159
|
-
|
160
|
-
# It tries to invoke a method for the view or a local for the given key.
|
161
|
-
# If the lookup fails, it returns a null object.
|
162
|
-
#
|
163
|
-
# @return [Object,Hanami::View::Rendering::NullLocal] the returning value
|
164
|
-
#
|
165
|
-
# @since 1.1.0
|
166
|
-
#
|
167
|
-
# @example Safe method navigation
|
168
|
-
# class ApplicationLayout
|
169
|
-
# include Hanami::Layout
|
170
|
-
#
|
171
|
-
# def render_flash
|
172
|
-
# return if local(:flash).nil?
|
173
|
-
#
|
174
|
-
# # ...
|
175
|
-
# end
|
176
|
-
# end
|
177
|
-
def local(key)
|
178
|
-
@scope.local(key)
|
179
|
-
end
|
180
|
-
|
181
|
-
protected
|
182
|
-
# The template for the current format
|
183
|
-
#
|
184
|
-
# @api private
|
185
|
-
# @since 0.1.0
|
186
|
-
def template
|
187
|
-
self.class.registry.resolve({format: @scope.format})
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|
data/lib/hanami/presenter.rb
DELETED
@@ -1,98 +0,0 @@
|
|
1
|
-
require 'hanami/view/escape'
|
2
|
-
|
3
|
-
module Hanami
|
4
|
-
# Presenter pattern implementation
|
5
|
-
#
|
6
|
-
# It delegates to the wrapped object the missing method invocations.
|
7
|
-
#
|
8
|
-
# The output of concrete and delegated methods is escaped as XSS prevention.
|
9
|
-
#
|
10
|
-
# @since 0.1.0
|
11
|
-
#
|
12
|
-
# @example Basic usage
|
13
|
-
# require 'hanami/view'
|
14
|
-
#
|
15
|
-
# class Map
|
16
|
-
# attr_reader :locations
|
17
|
-
#
|
18
|
-
# def initialize(locations)
|
19
|
-
# @locations = locations
|
20
|
-
# end
|
21
|
-
#
|
22
|
-
# def location_names
|
23
|
-
# @locations.join(', ')
|
24
|
-
# end
|
25
|
-
# end
|
26
|
-
#
|
27
|
-
# class MapPresenter
|
28
|
-
# include Hanami::Presenter
|
29
|
-
#
|
30
|
-
# def count
|
31
|
-
# locations.count
|
32
|
-
# end
|
33
|
-
#
|
34
|
-
# def location_names
|
35
|
-
# super.upcase
|
36
|
-
# end
|
37
|
-
#
|
38
|
-
# def inspect_object
|
39
|
-
# @object.inspect
|
40
|
-
# end
|
41
|
-
# end
|
42
|
-
#
|
43
|
-
# map = Map.new(['Rome', 'Boston'])
|
44
|
-
# presenter = MapPresenter.new(map)
|
45
|
-
#
|
46
|
-
# # access a map method
|
47
|
-
# puts presenter.locations # => ['Rome', 'Boston']
|
48
|
-
#
|
49
|
-
# # access presenter concrete methods
|
50
|
-
# puts presenter.count # => 1
|
51
|
-
#
|
52
|
-
# # uses super to access original object implementation
|
53
|
-
# puts presenter.location_names # => 'ROME, BOSTON'
|
54
|
-
#
|
55
|
-
# # it has private access to the original object
|
56
|
-
# puts presenter.inspect_object # => #<Map:0x007fdeada0b2f0 @locations=["Rome", "Boston"]>
|
57
|
-
#
|
58
|
-
# @example Escape
|
59
|
-
# require 'hanami/view'
|
60
|
-
#
|
61
|
-
# User = Struct.new(:first_name, :last_name)
|
62
|
-
#
|
63
|
-
# class UserPresenter
|
64
|
-
# include Hanami::Presenter
|
65
|
-
#
|
66
|
-
# def full_name
|
67
|
-
# [first_name, last_name].join(' ')
|
68
|
-
# end
|
69
|
-
#
|
70
|
-
# def raw_first_name
|
71
|
-
# _raw first_name
|
72
|
-
# end
|
73
|
-
# end
|
74
|
-
#
|
75
|
-
# first_name = '<script>alert('xss')</script>'
|
76
|
-
#
|
77
|
-
# user = User.new(first_name, nil)
|
78
|
-
# presenter = UserPresenter.new(user)
|
79
|
-
#
|
80
|
-
# presenter.full_name
|
81
|
-
# # => "<script>alert('xss')</script>"
|
82
|
-
#
|
83
|
-
# presenter.raw_full_name
|
84
|
-
# # => "<script>alert('xss')</script>"
|
85
|
-
module Presenter
|
86
|
-
# Inject escape logic into the given class.
|
87
|
-
#
|
88
|
-
# @since 0.4.0
|
89
|
-
# @api private
|
90
|
-
#
|
91
|
-
# @see Hanami::View::Escape
|
92
|
-
def self.included(base)
|
93
|
-
base.class_eval do
|
94
|
-
include ::Hanami::View::Escape::Presentable
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
@@ -1,504 +0,0 @@
|
|
1
|
-
require 'set'
|
2
|
-
require 'hanami/utils/class'
|
3
|
-
require 'hanami/utils/kernel'
|
4
|
-
require 'hanami/utils/string'
|
5
|
-
require 'hanami/utils/load_paths'
|
6
|
-
require 'hanami/view/rendering/layout_finder'
|
7
|
-
require 'hanami/view/rendering/partial_templates_finder'
|
8
|
-
|
9
|
-
module Hanami
|
10
|
-
module View
|
11
|
-
# Configuration for the framework, controllers and actions.
|
12
|
-
#
|
13
|
-
# Hanami::Controller has its own global configuration that can be manipulated
|
14
|
-
# via `Hanami::View.configure`.
|
15
|
-
#
|
16
|
-
# Every time that `Hanami::View` and `Hanami::Layout` are included, that
|
17
|
-
# global configuration is being copied to the recipient. The copy will
|
18
|
-
# inherit all the settings from the original, but all the subsequent changes
|
19
|
-
# aren't reflected from the parent to the children, and viceversa.
|
20
|
-
#
|
21
|
-
# This architecture allows to have a global configuration that capture the
|
22
|
-
# most common cases for an application, and let views and layouts
|
23
|
-
# layouts to specify exceptions.
|
24
|
-
#
|
25
|
-
# @since 0.2.0
|
26
|
-
class Configuration
|
27
|
-
# Default root
|
28
|
-
#
|
29
|
-
# @since 0.2.0
|
30
|
-
# @api private
|
31
|
-
DEFAULT_ROOT = '.'.freeze
|
32
|
-
|
33
|
-
# Default encoding
|
34
|
-
#
|
35
|
-
# @since 0.5.0
|
36
|
-
# @api private
|
37
|
-
DEFAULT_ENCODING = Encoding::UTF_8
|
38
|
-
|
39
|
-
attr_reader :load_paths
|
40
|
-
attr_reader :views
|
41
|
-
attr_reader :layouts
|
42
|
-
attr_reader :modules
|
43
|
-
attr_reader :partials
|
44
|
-
|
45
|
-
# Return the original configuration of the framework instance associated
|
46
|
-
# with the given class.
|
47
|
-
#
|
48
|
-
# When multiple instances of Hanami::View are used in the same application,
|
49
|
-
# we want to make sure that a controller or an action will receive the
|
50
|
-
# expected configuration.
|
51
|
-
#
|
52
|
-
# @param base [Class] a view or a layout
|
53
|
-
#
|
54
|
-
# @return [Hanami::Controller::Configuration] the configuration associated
|
55
|
-
# to the given class.
|
56
|
-
#
|
57
|
-
# @since 0.2.0
|
58
|
-
# @api private
|
59
|
-
#
|
60
|
-
# @example Direct usage of the framework
|
61
|
-
# require 'hanami/view'
|
62
|
-
#
|
63
|
-
# class Show
|
64
|
-
# include Hanami::View
|
65
|
-
# end
|
66
|
-
#
|
67
|
-
# Hanami::View::Configuration.for(Show)
|
68
|
-
# # => will return from Hanami::View
|
69
|
-
#
|
70
|
-
# @example Multiple instances of the framework
|
71
|
-
# require 'hanami/view'
|
72
|
-
#
|
73
|
-
# module MyApp
|
74
|
-
# View = Hanami::View.duplicate(self)
|
75
|
-
#
|
76
|
-
# module Views::Dashboard
|
77
|
-
# class Index
|
78
|
-
# include MyApp::View
|
79
|
-
# end
|
80
|
-
# end
|
81
|
-
# end
|
82
|
-
#
|
83
|
-
# class Show
|
84
|
-
# include Hanami::Action
|
85
|
-
# end
|
86
|
-
#
|
87
|
-
# Hanami::View::Configuration.for(Show)
|
88
|
-
# # => will return from Hanami::View
|
89
|
-
#
|
90
|
-
# Hanami::View::Configuration.for(MyApp::Views::Dashboard::Index)
|
91
|
-
# # => will return from MyApp::View
|
92
|
-
def self.for(base)
|
93
|
-
# TODO this implementation is similar to Hanami::Controller::Configuration consider to extract it into Hanami::Utils
|
94
|
-
namespace = Utils::String.namespace(base)
|
95
|
-
framework = Utils::Class.load("#{namespace}::View") || Utils::Class.load!('Hanami::View')
|
96
|
-
framework.configuration
|
97
|
-
end
|
98
|
-
|
99
|
-
# Initialize a configuration instance
|
100
|
-
#
|
101
|
-
# @return [Hanami::View::Configuration] a new configuration's instance
|
102
|
-
#
|
103
|
-
# @since 0.2.0
|
104
|
-
def initialize
|
105
|
-
@namespace = Object
|
106
|
-
reset!
|
107
|
-
end
|
108
|
-
|
109
|
-
# Set the Ruby namespace where to lookup for views.
|
110
|
-
#
|
111
|
-
# When multiple instances of the framework are used, we want to make sure
|
112
|
-
# that if a `MyApp` wants a `Dashboard::Index` view, we are loading the
|
113
|
-
# right one.
|
114
|
-
#
|
115
|
-
# If not set, this value defaults to `Object`.
|
116
|
-
#
|
117
|
-
# This is part of a DSL, for this reason when this method is called with
|
118
|
-
# an argument, it will set the corresponding instance variable. When
|
119
|
-
# called without, it will return the already set value, or the default.
|
120
|
-
#
|
121
|
-
# @overload namespace(value)
|
122
|
-
# Sets the given value
|
123
|
-
# @param value [Class, Module, String] a valid Ruby namespace identifier
|
124
|
-
#
|
125
|
-
# @overload namespace
|
126
|
-
# Gets the value
|
127
|
-
# @return [Class, Module, String]
|
128
|
-
#
|
129
|
-
# @since 0.2.0
|
130
|
-
#
|
131
|
-
# @example Getting the value
|
132
|
-
# require 'hanami/view'
|
133
|
-
#
|
134
|
-
# Hanami::View.configuration.namespace # => Object
|
135
|
-
#
|
136
|
-
# @example Setting the value
|
137
|
-
# require 'hanami/view'
|
138
|
-
#
|
139
|
-
# Hanami::View.configure do
|
140
|
-
# namespace 'MyApp::Views'
|
141
|
-
# end
|
142
|
-
def namespace(value = nil)
|
143
|
-
if value
|
144
|
-
@namespace = value
|
145
|
-
else
|
146
|
-
@namespace
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
# Set the root path where to search for templates
|
151
|
-
#
|
152
|
-
# If not set, this value defaults to the current directory.
|
153
|
-
#
|
154
|
-
# This is part of a DSL, for this reason when this method is called with
|
155
|
-
# an argument, it will set the corresponding instance variable. When
|
156
|
-
# called without, it will return the already set value, or the default.
|
157
|
-
#
|
158
|
-
# @overload root(value)
|
159
|
-
# Sets the given value
|
160
|
-
# @param value [String,Pathname,#to_pathname] an object that can be
|
161
|
-
# coerced to Pathname
|
162
|
-
# @raise [Errno::ENOENT] if the given path doesn't exist
|
163
|
-
#
|
164
|
-
# @overload root
|
165
|
-
# Gets the value
|
166
|
-
# @return [Pathname]
|
167
|
-
#
|
168
|
-
# @since 0.2.0
|
169
|
-
#
|
170
|
-
# @see Hanami::View::Dsl#root
|
171
|
-
# @see http://www.ruby-doc.org/stdlib-2.1.2/libdoc/pathname/rdoc/Pathname.html
|
172
|
-
# @see http://rdoc.info/gems/hanami-utils/Hanami/Utils/Kernel#Pathname-class_method
|
173
|
-
#
|
174
|
-
# @example Getting the value
|
175
|
-
# require 'hanami/view'
|
176
|
-
#
|
177
|
-
# Hanami::View.configuration.root # => #<Pathname:.>
|
178
|
-
#
|
179
|
-
# @example Setting the value
|
180
|
-
# require 'hanami/view'
|
181
|
-
#
|
182
|
-
# Hanami::View.configure do
|
183
|
-
# root '/path/to/templates'
|
184
|
-
# end
|
185
|
-
#
|
186
|
-
# Hanami::View.configuration.root # => #<Pathname:/path/to/templates>
|
187
|
-
def root(value = nil)
|
188
|
-
if value
|
189
|
-
@root = Utils::Kernel.Pathname(value).realpath
|
190
|
-
else
|
191
|
-
@root
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
# Set the global layout
|
196
|
-
#
|
197
|
-
# If not set, this value defaults to `nil`, while at the rendering time
|
198
|
-
# it will use `Hanami::View::Rendering::NullLayout`.
|
199
|
-
#
|
200
|
-
# This is part of a DSL, for this reason when this method is called with
|
201
|
-
# an argument, it will set the corresponding instance variable. When
|
202
|
-
# called without, it will return the already set value, or the default.
|
203
|
-
#
|
204
|
-
# @overload layout(value)
|
205
|
-
# Sets the given value
|
206
|
-
# @param value [Symbol] the name of the layout
|
207
|
-
#
|
208
|
-
# @overload layout
|
209
|
-
# Gets the value
|
210
|
-
# @return [Class]
|
211
|
-
#
|
212
|
-
# @since 0.2.0
|
213
|
-
#
|
214
|
-
# @see Hanami::View::Dsl#layout
|
215
|
-
#
|
216
|
-
# @example Getting the value
|
217
|
-
# require 'hanami/view'
|
218
|
-
#
|
219
|
-
# Hanami::View.configuration.layout # => nil
|
220
|
-
#
|
221
|
-
# @example Setting the value
|
222
|
-
# require 'hanami/view'
|
223
|
-
#
|
224
|
-
# Hanami::View.configure do
|
225
|
-
# layout :application
|
226
|
-
# end
|
227
|
-
#
|
228
|
-
# Hanami::View.configuration.layout # => ApplicationLayout
|
229
|
-
#
|
230
|
-
# @example Setting the value in a namespaced app
|
231
|
-
# require 'hanami/view'
|
232
|
-
#
|
233
|
-
# module MyApp
|
234
|
-
# View = Hanami::View.duplicate(self) do
|
235
|
-
# layout :application
|
236
|
-
# end
|
237
|
-
# end
|
238
|
-
#
|
239
|
-
# MyApp::View.configuration.layout # => MyApp::ApplicationLayout
|
240
|
-
def layout(value = nil)
|
241
|
-
if value.nil?
|
242
|
-
Rendering::LayoutFinder.find(@layout, @namespace)
|
243
|
-
else
|
244
|
-
@layout = value
|
245
|
-
end
|
246
|
-
end
|
247
|
-
|
248
|
-
# Default encoding for templates
|
249
|
-
#
|
250
|
-
# This is part of a DSL, for this reason when this method is called with
|
251
|
-
# an argument, it will set the corresponding instance variable. When
|
252
|
-
# called without, it will return the already set value, or the default.
|
253
|
-
#
|
254
|
-
# @overload default_encoding(value)
|
255
|
-
# Sets the given value
|
256
|
-
# @param value [String,Encoding] a string representation of the encoding,
|
257
|
-
# or an Encoding constant
|
258
|
-
#
|
259
|
-
# @raise [ArgumentError] if the given value isn't a supported encoding
|
260
|
-
#
|
261
|
-
# @overload default_encoding
|
262
|
-
# Gets the value
|
263
|
-
# @return [Encoding]
|
264
|
-
#
|
265
|
-
# @since 0.5.0
|
266
|
-
#
|
267
|
-
# @example Set UTF-8 As A String
|
268
|
-
# require 'hanami/view'
|
269
|
-
#
|
270
|
-
# Hanami::View.configure do
|
271
|
-
# default_encoding 'utf-8'
|
272
|
-
# end
|
273
|
-
#
|
274
|
-
# @example Set UTF-8 As An Encoding Constant
|
275
|
-
# require 'hanami/view'
|
276
|
-
#
|
277
|
-
# Hanami::View.configure do
|
278
|
-
# default_encoding Encoding::UTF_8
|
279
|
-
# end
|
280
|
-
#
|
281
|
-
# @example Raise An Error For Unknown Encoding
|
282
|
-
# require 'hanami/view'
|
283
|
-
#
|
284
|
-
# Hanami::View.configure do
|
285
|
-
# default_encoding 'foo'
|
286
|
-
# end
|
287
|
-
#
|
288
|
-
# # => ArgumentError
|
289
|
-
def default_encoding(value = nil)
|
290
|
-
if value.nil?
|
291
|
-
@default_encoding
|
292
|
-
else
|
293
|
-
@default_encoding = Encoding.find(value)
|
294
|
-
end
|
295
|
-
end
|
296
|
-
|
297
|
-
# Prepare the views.
|
298
|
-
#
|
299
|
-
# The given block will be yielded when `Hanami::View` will be included by
|
300
|
-
# a view.
|
301
|
-
#
|
302
|
-
# This method can be called multiple times.
|
303
|
-
#
|
304
|
-
# @param blk [Proc] the code block
|
305
|
-
#
|
306
|
-
# @return [void]
|
307
|
-
#
|
308
|
-
# @raise [ArgumentError] if called without passing a block
|
309
|
-
#
|
310
|
-
# @since 0.3.0
|
311
|
-
#
|
312
|
-
# @see Hanami::View.configure
|
313
|
-
# @see Hanami::View.duplicate
|
314
|
-
#
|
315
|
-
# @example Including shared utilities
|
316
|
-
# require 'hanami/view'
|
317
|
-
#
|
318
|
-
# module UrlHelpers
|
319
|
-
# def comments_path
|
320
|
-
# '/'
|
321
|
-
# end
|
322
|
-
# end
|
323
|
-
#
|
324
|
-
# Hanami::View.configure do
|
325
|
-
# prepare do
|
326
|
-
# include UrlHelpers
|
327
|
-
# end
|
328
|
-
# end
|
329
|
-
#
|
330
|
-
# Hanami::View.load!
|
331
|
-
#
|
332
|
-
# module Comments
|
333
|
-
# class New
|
334
|
-
# # The following include will cause UrlHelpers to be included too.
|
335
|
-
# # This makes `comments_path` available in the view context
|
336
|
-
# include Hanami::View
|
337
|
-
#
|
338
|
-
# def form
|
339
|
-
# %(<form action="#{ comments_path }" method="POST"></form>)
|
340
|
-
# end
|
341
|
-
# end
|
342
|
-
# end
|
343
|
-
#
|
344
|
-
# @example Preparing multiple times
|
345
|
-
# require 'hanami/view'
|
346
|
-
#
|
347
|
-
# Hanami::View.configure do
|
348
|
-
# prepare do
|
349
|
-
# include UrlHelpers
|
350
|
-
# end
|
351
|
-
#
|
352
|
-
# prepare do
|
353
|
-
# format :json
|
354
|
-
# end
|
355
|
-
# end
|
356
|
-
#
|
357
|
-
# Hanami::View.configure do
|
358
|
-
# prepare do
|
359
|
-
# include FormattingHelpers
|
360
|
-
# end
|
361
|
-
# end
|
362
|
-
#
|
363
|
-
# Hanami::View.load!
|
364
|
-
#
|
365
|
-
# module Articles
|
366
|
-
# class Index
|
367
|
-
# # The following include will cause the inclusion of:
|
368
|
-
# # * UrlHelpers
|
369
|
-
# # * FormattingHelpers
|
370
|
-
# #
|
371
|
-
# # It also sets the view to render only JSON
|
372
|
-
# include Hanami::View
|
373
|
-
# end
|
374
|
-
# end
|
375
|
-
def prepare(&blk)
|
376
|
-
if block_given?
|
377
|
-
@modules.push(blk)
|
378
|
-
else
|
379
|
-
raise ArgumentError.new('Please provide a block')
|
380
|
-
end
|
381
|
-
end
|
382
|
-
|
383
|
-
# Add a view to the registry
|
384
|
-
#
|
385
|
-
# @since 0.2.0
|
386
|
-
# @api private
|
387
|
-
def add_view(view)
|
388
|
-
@views.add(view)
|
389
|
-
end
|
390
|
-
|
391
|
-
# Add a layout to the registry
|
392
|
-
#
|
393
|
-
# @since 0.2.0
|
394
|
-
# @api private
|
395
|
-
def add_layout(layout)
|
396
|
-
@layouts.add(layout)
|
397
|
-
end
|
398
|
-
|
399
|
-
# Duplicate by copying the settings in a new instance.
|
400
|
-
#
|
401
|
-
# @return [Hanami::View::Configuration] a copy of the configuration
|
402
|
-
#
|
403
|
-
# @since 0.2.0
|
404
|
-
# @api private
|
405
|
-
def duplicate
|
406
|
-
Configuration.new.tap do |c|
|
407
|
-
c.namespace = namespace
|
408
|
-
c.root = root
|
409
|
-
c.layout = @layout # lazy loading of the class
|
410
|
-
c.default_encoding = default_encoding
|
411
|
-
c.load_paths = load_paths.dup
|
412
|
-
c.modules = modules.dup
|
413
|
-
end
|
414
|
-
end
|
415
|
-
|
416
|
-
# Load the configuration for the current framework
|
417
|
-
#
|
418
|
-
# @since 0.2.0
|
419
|
-
# @api private
|
420
|
-
def load!
|
421
|
-
views.each { |v| v.__send__(:load!) }
|
422
|
-
layouts.each { |l| l.__send__(:load!) }
|
423
|
-
load_partials!
|
424
|
-
freeze
|
425
|
-
end
|
426
|
-
|
427
|
-
# Load partials for each partial template file found under the
|
428
|
-
# given load paths
|
429
|
-
#
|
430
|
-
# @since 0.7.0
|
431
|
-
# @api private
|
432
|
-
def load_partials!
|
433
|
-
Hanami::View::Rendering::PartialTemplatesFinder.new(self).find.each do |partial|
|
434
|
-
add_partial(partial)
|
435
|
-
end
|
436
|
-
end
|
437
|
-
|
438
|
-
# Load partials for each partial template file found under the
|
439
|
-
# given load paths
|
440
|
-
#
|
441
|
-
# @since 0.7.0
|
442
|
-
# @api private
|
443
|
-
def find_partial(relative_partial_path, template_name, format)
|
444
|
-
partials_for_view = partials.has_key?(relative_partial_path) ? partials[relative_partial_path] : partials[template_name]
|
445
|
-
partials_for_view ? partials_for_view[format.to_sym] : nil
|
446
|
-
end
|
447
|
-
|
448
|
-
# Add a partial to the registry
|
449
|
-
#
|
450
|
-
# @since 0.7.0
|
451
|
-
# @api private
|
452
|
-
def add_partial(partial)
|
453
|
-
@partials[partial.key][partial.format.to_sym] = partial.template
|
454
|
-
end
|
455
|
-
|
456
|
-
# Reset all the values to the defaults
|
457
|
-
#
|
458
|
-
# @since 0.2.0
|
459
|
-
# @api private
|
460
|
-
def reset!
|
461
|
-
root DEFAULT_ROOT
|
462
|
-
default_encoding DEFAULT_ENCODING
|
463
|
-
|
464
|
-
@partials = Hash.new { |h, k| h[k] = Hash.new }
|
465
|
-
@views = Set.new
|
466
|
-
@layouts = Set.new
|
467
|
-
@load_paths = Utils::LoadPaths.new(root)
|
468
|
-
@layout = nil
|
469
|
-
@modules = []
|
470
|
-
end
|
471
|
-
|
472
|
-
# Copy the configuration for the given action
|
473
|
-
#
|
474
|
-
# @param base [Class] the target action
|
475
|
-
#
|
476
|
-
# @return void
|
477
|
-
#
|
478
|
-
# @since 0.3.0
|
479
|
-
# @api private
|
480
|
-
def copy!(base)
|
481
|
-
modules.each do |mod|
|
482
|
-
base.class_eval(&mod)
|
483
|
-
end
|
484
|
-
end
|
485
|
-
|
486
|
-
# @api private
|
487
|
-
alias_method :unload!, :reset!
|
488
|
-
|
489
|
-
protected
|
490
|
-
# @api private
|
491
|
-
attr_writer :namespace
|
492
|
-
# @api private
|
493
|
-
attr_writer :root
|
494
|
-
# @api private
|
495
|
-
attr_writer :load_paths
|
496
|
-
# @api private
|
497
|
-
attr_writer :layout
|
498
|
-
# @api private
|
499
|
-
attr_writer :default_encoding
|
500
|
-
# @api private
|
501
|
-
attr_writer :modules
|
502
|
-
end
|
503
|
-
end
|
504
|
-
end
|