padrino-core 0.12.0 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/padrino-core.rb +4 -4
- data/lib/padrino-core/application.rb +6 -195
- data/lib/padrino-core/application/application_setup.rb +199 -0
- data/lib/padrino-core/application/routing.rb +66 -25
- data/lib/padrino-core/cli/base.rb +8 -5
- data/lib/padrino-core/cli/rake.rb +12 -9
- data/lib/padrino-core/loader.rb +1 -1
- data/lib/padrino-core/logger.rb +25 -0
- data/lib/padrino-core/mounter.rb +8 -3
- data/lib/padrino-core/reloader.rb +2 -2
- data/lib/padrino-core/server.rb +50 -17
- data/lib/padrino-core/version.rb +1 -1
- data/padrino-core.gemspec +2 -10
- data/test/fixtures/apps/demo_app.rb +7 -0
- data/test/fixtures/apps/demo_demo.rb +7 -0
- data/test/helper.rb +6 -37
- data/test/test_application.rb +12 -13
- data/test/test_core.rb +12 -13
- data/test/test_csrf_protection.rb +49 -23
- data/test/test_dependencies.rb +7 -7
- data/test/test_filters.rb +41 -17
- data/test/test_flash.rb +24 -24
- data/test/test_locale.rb +1 -1
- data/test/test_logger.rb +39 -27
- data/test/test_mounter.rb +34 -20
- data/test/test_reloader_complex.rb +5 -6
- data/test/test_reloader_simple.rb +23 -20
- data/test/test_reloader_system.rb +10 -7
- data/test/test_restful_routing.rb +1 -1
- data/test/test_router.rb +7 -7
- data/test/test_routing.rb +177 -141
- metadata +14 -53
- data/lib/padrino-core/application/rendering.rb +0 -325
- data/lib/padrino-core/application/rendering/extensions/erubis.rb +0 -68
- data/lib/padrino-core/application/rendering/extensions/haml.rb +0 -29
- data/lib/padrino-core/application/rendering/extensions/slim.rb +0 -21
- data/lib/padrino-core/locale/cs.yml +0 -33
- data/lib/padrino-core/locale/da.yml +0 -33
- data/lib/padrino-core/locale/de.yml +0 -33
- data/lib/padrino-core/locale/en.yml +0 -33
- data/lib/padrino-core/locale/es.yml +0 -33
- data/lib/padrino-core/locale/fr.yml +0 -33
- data/lib/padrino-core/locale/hu.yml +0 -33
- data/lib/padrino-core/locale/it.yml +0 -39
- data/lib/padrino-core/locale/ja.yml +0 -33
- data/lib/padrino-core/locale/lv.yml +0 -33
- data/lib/padrino-core/locale/nl.yml +0 -33
- data/lib/padrino-core/locale/no.yml +0 -33
- data/lib/padrino-core/locale/pl.yml +0 -33
- data/lib/padrino-core/locale/pt_br.yml +0 -39
- data/lib/padrino-core/locale/ro.yml +0 -33
- data/lib/padrino-core/locale/ru.yml +0 -34
- data/lib/padrino-core/locale/sv.yml +0 -33
- data/lib/padrino-core/locale/tr.yml +0 -33
- data/lib/padrino-core/locale/uk.yml +0 -33
- data/lib/padrino-core/locale/zh_cn.yml +0 -33
- data/lib/padrino-core/locale/zh_tw.yml +0 -33
- data/lib/padrino-core/support_lite.rb +0 -259
- data/test/fixtures/apps/.components +0 -6
- data/test/fixtures/apps/.gitignore +0 -7
- data/test/fixtures/apps/render.rb +0 -13
- data/test/fixtures/apps/views/blog/post.erb +0 -1
- data/test/fixtures/layouts/layout.erb +0 -1
- data/test/mini_shoulda.rb +0 -45
- data/test/test_rendering.rb +0 -606
- data/test/test_rendering_extensions.rb +0 -14
- data/test/test_support_lite.rb +0 -56
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: padrino-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.12.
|
4
|
+
version: 0.12.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Padrino Team
|
@@ -11,22 +11,22 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2014-
|
14
|
+
date: 2014-04-04 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
|
-
name:
|
17
|
+
name: padrino-support
|
18
18
|
requirement: !ruby/object:Gem::Requirement
|
19
19
|
requirements:
|
20
|
-
- -
|
20
|
+
- - '='
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
22
|
+
version: 0.12.1
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - '='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
29
|
+
version: 0.12.1
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: sinatra
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -61,14 +61,14 @@ dependencies:
|
|
61
61
|
requirements:
|
62
62
|
- - ~>
|
63
63
|
- !ruby/object:Gem::Version
|
64
|
-
version: 0.
|
64
|
+
version: 0.18.0
|
65
65
|
type: :runtime
|
66
66
|
prerelease: false
|
67
67
|
version_requirements: !ruby/object:Gem::Requirement
|
68
68
|
requirements:
|
69
69
|
- - ~>
|
70
70
|
- !ruby/object:Gem::Version
|
71
|
-
version: 0.
|
71
|
+
version: 0.18.0
|
72
72
|
- !ruby/object:Gem::Dependency
|
73
73
|
name: activesupport
|
74
74
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,12 +114,9 @@ files:
|
|
114
114
|
- bin/padrino
|
115
115
|
- lib/padrino-core.rb
|
116
116
|
- lib/padrino-core/application.rb
|
117
|
+
- lib/padrino-core/application/application_setup.rb
|
117
118
|
- lib/padrino-core/application/authenticity_token.rb
|
118
119
|
- lib/padrino-core/application/flash.rb
|
119
|
-
- lib/padrino-core/application/rendering.rb
|
120
|
-
- lib/padrino-core/application/rendering/extensions/erubis.rb
|
121
|
-
- lib/padrino-core/application/rendering/extensions/haml.rb
|
122
|
-
- lib/padrino-core/application/rendering/extensions/slim.rb
|
123
120
|
- lib/padrino-core/application/routing.rb
|
124
121
|
- lib/padrino-core/application/show_exceptions.rb
|
125
122
|
- lib/padrino-core/caller.rb
|
@@ -132,27 +129,6 @@ files:
|
|
132
129
|
- lib/padrino-core/images/404.png
|
133
130
|
- lib/padrino-core/images/500.png
|
134
131
|
- lib/padrino-core/loader.rb
|
135
|
-
- lib/padrino-core/locale/cs.yml
|
136
|
-
- lib/padrino-core/locale/da.yml
|
137
|
-
- lib/padrino-core/locale/de.yml
|
138
|
-
- lib/padrino-core/locale/en.yml
|
139
|
-
- lib/padrino-core/locale/es.yml
|
140
|
-
- lib/padrino-core/locale/fr.yml
|
141
|
-
- lib/padrino-core/locale/hu.yml
|
142
|
-
- lib/padrino-core/locale/it.yml
|
143
|
-
- lib/padrino-core/locale/ja.yml
|
144
|
-
- lib/padrino-core/locale/lv.yml
|
145
|
-
- lib/padrino-core/locale/nl.yml
|
146
|
-
- lib/padrino-core/locale/no.yml
|
147
|
-
- lib/padrino-core/locale/pl.yml
|
148
|
-
- lib/padrino-core/locale/pt_br.yml
|
149
|
-
- lib/padrino-core/locale/ro.yml
|
150
|
-
- lib/padrino-core/locale/ru.yml
|
151
|
-
- lib/padrino-core/locale/sv.yml
|
152
|
-
- lib/padrino-core/locale/tr.yml
|
153
|
-
- lib/padrino-core/locale/uk.yml
|
154
|
-
- lib/padrino-core/locale/zh_cn.yml
|
155
|
-
- lib/padrino-core/locale/zh_tw.yml
|
156
132
|
- lib/padrino-core/logger.rb
|
157
133
|
- lib/padrino-core/module.rb
|
158
134
|
- lib/padrino-core/mounter.rb
|
@@ -161,7 +137,6 @@ files:
|
|
161
137
|
- lib/padrino-core/reloader/storage.rb
|
162
138
|
- lib/padrino-core/router.rb
|
163
139
|
- lib/padrino-core/server.rb
|
164
|
-
- lib/padrino-core/support_lite.rb
|
165
140
|
- lib/padrino-core/tasks.rb
|
166
141
|
- lib/padrino-core/version.rb
|
167
142
|
- padrino-core.gemspec
|
@@ -170,20 +145,18 @@ files:
|
|
170
145
|
- test/fixtures/app_gem/app_gem.gemspec
|
171
146
|
- test/fixtures/app_gem/lib/app_gem.rb
|
172
147
|
- test/fixtures/app_gem/lib/app_gem/version.rb
|
173
|
-
- test/fixtures/apps/.components
|
174
|
-
- test/fixtures/apps/.gitignore
|
175
148
|
- test/fixtures/apps/complex.rb
|
149
|
+
- test/fixtures/apps/demo_app.rb
|
150
|
+
- test/fixtures/apps/demo_demo.rb
|
176
151
|
- test/fixtures/apps/helpers/system_helpers.rb
|
177
152
|
- test/fixtures/apps/kiq.rb
|
178
153
|
- test/fixtures/apps/lib/myklass.rb
|
179
154
|
- test/fixtures/apps/lib/myklass/mysubklass.rb
|
180
155
|
- test/fixtures/apps/models/child.rb
|
181
156
|
- test/fixtures/apps/models/parent.rb
|
182
|
-
- test/fixtures/apps/render.rb
|
183
157
|
- test/fixtures/apps/simple.rb
|
184
158
|
- test/fixtures/apps/static.rb
|
185
159
|
- test/fixtures/apps/system.rb
|
186
|
-
- test/fixtures/apps/views/blog/post.erb
|
187
160
|
- test/fixtures/dependencies/a.rb
|
188
161
|
- test/fixtures/dependencies/b.rb
|
189
162
|
- test/fixtures/dependencies/c.rb
|
@@ -191,9 +164,7 @@ files:
|
|
191
164
|
- test/fixtures/dependencies/circular/f.rb
|
192
165
|
- test/fixtures/dependencies/circular/g.rb
|
193
166
|
- test/fixtures/dependencies/d.rb
|
194
|
-
- test/fixtures/layouts/layout.erb
|
195
167
|
- test/helper.rb
|
196
|
-
- test/mini_shoulda.rb
|
197
168
|
- test/test_application.rb
|
198
169
|
- test/test_core.rb
|
199
170
|
- test/test_csrf_protection.rb
|
@@ -206,12 +177,9 @@ files:
|
|
206
177
|
- test/test_reloader_complex.rb
|
207
178
|
- test/test_reloader_simple.rb
|
208
179
|
- test/test_reloader_system.rb
|
209
|
-
- test/test_rendering.rb
|
210
|
-
- test/test_rendering_extensions.rb
|
211
180
|
- test/test_restful_routing.rb
|
212
181
|
- test/test_router.rb
|
213
182
|
- test/test_routing.rb
|
214
|
-
- test/test_support_lite.rb
|
215
183
|
homepage: http://www.padrinorb.com
|
216
184
|
licenses:
|
217
185
|
- MIT
|
@@ -243,20 +211,18 @@ test_files:
|
|
243
211
|
- test/fixtures/app_gem/app_gem.gemspec
|
244
212
|
- test/fixtures/app_gem/lib/app_gem.rb
|
245
213
|
- test/fixtures/app_gem/lib/app_gem/version.rb
|
246
|
-
- test/fixtures/apps/.components
|
247
|
-
- test/fixtures/apps/.gitignore
|
248
214
|
- test/fixtures/apps/complex.rb
|
215
|
+
- test/fixtures/apps/demo_app.rb
|
216
|
+
- test/fixtures/apps/demo_demo.rb
|
249
217
|
- test/fixtures/apps/helpers/system_helpers.rb
|
250
218
|
- test/fixtures/apps/kiq.rb
|
251
219
|
- test/fixtures/apps/lib/myklass.rb
|
252
220
|
- test/fixtures/apps/lib/myklass/mysubklass.rb
|
253
221
|
- test/fixtures/apps/models/child.rb
|
254
222
|
- test/fixtures/apps/models/parent.rb
|
255
|
-
- test/fixtures/apps/render.rb
|
256
223
|
- test/fixtures/apps/simple.rb
|
257
224
|
- test/fixtures/apps/static.rb
|
258
225
|
- test/fixtures/apps/system.rb
|
259
|
-
- test/fixtures/apps/views/blog/post.erb
|
260
226
|
- test/fixtures/dependencies/a.rb
|
261
227
|
- test/fixtures/dependencies/b.rb
|
262
228
|
- test/fixtures/dependencies/c.rb
|
@@ -264,9 +230,7 @@ test_files:
|
|
264
230
|
- test/fixtures/dependencies/circular/f.rb
|
265
231
|
- test/fixtures/dependencies/circular/g.rb
|
266
232
|
- test/fixtures/dependencies/d.rb
|
267
|
-
- test/fixtures/layouts/layout.erb
|
268
233
|
- test/helper.rb
|
269
|
-
- test/mini_shoulda.rb
|
270
234
|
- test/test_application.rb
|
271
235
|
- test/test_core.rb
|
272
236
|
- test/test_csrf_protection.rb
|
@@ -279,10 +243,7 @@ test_files:
|
|
279
243
|
- test/test_reloader_complex.rb
|
280
244
|
- test/test_reloader_simple.rb
|
281
245
|
- test/test_reloader_system.rb
|
282
|
-
- test/test_rendering.rb
|
283
|
-
- test/test_rendering_extensions.rb
|
284
246
|
- test/test_restful_routing.rb
|
285
247
|
- test/test_router.rb
|
286
248
|
- test/test_routing.rb
|
287
|
-
- test/test_support_lite.rb
|
288
249
|
has_rdoc:
|
@@ -1,325 +0,0 @@
|
|
1
|
-
require 'padrino-core/support_lite' unless defined?(SupportLite)
|
2
|
-
|
3
|
-
module Padrino
|
4
|
-
##
|
5
|
-
# Padrino enhances the Sinatra 'render' method to have support for
|
6
|
-
# automatic template engine detection, enhanced layout functionality,
|
7
|
-
# locale enabled rendering, among other features.
|
8
|
-
#
|
9
|
-
module Rendering
|
10
|
-
##
|
11
|
-
# A SafeTemplate assumes that its output is safe.
|
12
|
-
#
|
13
|
-
module SafeTemplate
|
14
|
-
def render(*)
|
15
|
-
super.html_safe
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
##
|
20
|
-
# Exception responsible for when an expected template did not exist.
|
21
|
-
#
|
22
|
-
class TemplateNotFound < RuntimeError
|
23
|
-
end
|
24
|
-
|
25
|
-
##
|
26
|
-
# This is an array of file patterns to ignore. If your editor add a
|
27
|
-
# suffix during editing to your files please add it like:
|
28
|
-
#
|
29
|
-
# @example
|
30
|
-
# Padrino::Rendering::IGNORE_FILE_PATTERN << /~$/
|
31
|
-
#
|
32
|
-
IGNORE_FILE_PATTERN = [
|
33
|
-
/~$/ # This is for Gedit
|
34
|
-
] unless defined?(IGNORE_FILE_PATTERN)
|
35
|
-
|
36
|
-
##
|
37
|
-
# Default options used in the resolve_template-method.
|
38
|
-
#
|
39
|
-
DEFAULT_RENDERING_OPTIONS = { :strict_format => false, :raise_exceptions => true } unless defined?(DEFAULT_RENDERING_OPTIONS)
|
40
|
-
|
41
|
-
class << self
|
42
|
-
##
|
43
|
-
# Default engine configurations for Padrino::Rendering.
|
44
|
-
#
|
45
|
-
# @return {Hash<Symbol,Hash>}
|
46
|
-
# The configurations, keyed by engine.
|
47
|
-
def engine_configurations
|
48
|
-
@engine_configurations ||= {}
|
49
|
-
end
|
50
|
-
|
51
|
-
def registered(app)
|
52
|
-
included(app)
|
53
|
-
engine_configurations.each do |engine, configs|
|
54
|
-
app.set engine, configs
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
def included(base)
|
59
|
-
base.send(:include, InstanceMethods)
|
60
|
-
base.extend(ClassMethods)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
##
|
65
|
-
# Class methods responsible for rendering templates as part of a request.
|
66
|
-
#
|
67
|
-
module ClassMethods
|
68
|
-
##
|
69
|
-
# Use layout like rails does or if a block given then like sinatra.
|
70
|
-
# If used without a block, sets the current layout for the route.
|
71
|
-
#
|
72
|
-
# By default, searches in your:
|
73
|
-
#
|
74
|
-
# +app+/+views+/+layouts+/+application+.(+haml+|+erb+|+xxx+)
|
75
|
-
# +app+/+views+/+layout_name+.(+haml+|+erb+|+xxx+)
|
76
|
-
#
|
77
|
-
# If you define +layout+ :+custom+ then searches for your layouts in
|
78
|
-
# +app+/+views+/+layouts+/+custom+.(+haml+|+erb+|+xxx+)
|
79
|
-
# +app+/+views+/+custom+.(+haml+|+erb+|+xxx+)
|
80
|
-
#
|
81
|
-
# @param [Symbol] name (:layout)
|
82
|
-
# The layout to use.
|
83
|
-
#
|
84
|
-
# @yield []
|
85
|
-
#
|
86
|
-
def layout(name=:layout, &block)
|
87
|
-
return super(name, &block) if block_given?
|
88
|
-
@layout = name
|
89
|
-
end
|
90
|
-
|
91
|
-
##
|
92
|
-
# Returns the cached template file to render for a given url,
|
93
|
-
# content_type and locale.
|
94
|
-
#
|
95
|
-
# @param [Array<template_path, content_type, locale>] render_options
|
96
|
-
#
|
97
|
-
def fetch_template_file(render_options)
|
98
|
-
(@_cached_templates ||= {})[render_options]
|
99
|
-
end
|
100
|
-
|
101
|
-
##
|
102
|
-
# Caches the template file for the given rendering options.
|
103
|
-
#
|
104
|
-
# @param [String] template_file
|
105
|
-
# The path of the template file.
|
106
|
-
#
|
107
|
-
# @param [Array<template_path, content_type, locale>] render_options
|
108
|
-
#
|
109
|
-
def cache_template_file!(template_file, render_options)
|
110
|
-
(@_cached_templates ||= {})[render_options] = template_file || []
|
111
|
-
end
|
112
|
-
|
113
|
-
##
|
114
|
-
# Returns the cached layout path.
|
115
|
-
#
|
116
|
-
# @param [Symbol, nil] given_layout
|
117
|
-
# The requested layout.
|
118
|
-
#
|
119
|
-
def fetch_layout_path(given_layout=nil)
|
120
|
-
layout_name = given_layout || @layout || :application
|
121
|
-
@_cached_layout ||= {}
|
122
|
-
cached_layout_path = @_cached_layout[layout_name]
|
123
|
-
return cached_layout_path if cached_layout_path
|
124
|
-
has_layout_at_root = Dir["#{views}/#{layout_name}.*"].any?
|
125
|
-
layout_path = has_layout_at_root ? layout_name.to_sym : File.join('layouts', layout_name.to_s).to_sym
|
126
|
-
@_cached_layout[layout_name] = layout_path unless reload_templates?
|
127
|
-
layout_path
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
# Instance methods that allow enhanced rendering to function properly in Padrino.
|
132
|
-
module InstanceMethods
|
133
|
-
attr_reader :current_engine
|
134
|
-
|
135
|
-
##
|
136
|
-
# Get/Set the content_type
|
137
|
-
#
|
138
|
-
# @param [String, nil] type
|
139
|
-
# The Content-Type to use.
|
140
|
-
#
|
141
|
-
# @param [Symbol, nil] type.
|
142
|
-
# Look and parse the given symbol to the matched Content-Type.
|
143
|
-
#
|
144
|
-
# @param [Hash] params
|
145
|
-
# Additional params to append to the Content-Type.
|
146
|
-
#
|
147
|
-
# @example
|
148
|
-
# case content_type
|
149
|
-
# when :js then do_some
|
150
|
-
# when :css then do_another
|
151
|
-
# end
|
152
|
-
#
|
153
|
-
# content_type :js
|
154
|
-
# # => set the response with 'application/javascript' Content-Type
|
155
|
-
# content_type 'text/html'
|
156
|
-
#
|
157
|
-
# # => set directly the Content-Type to 'text/html'
|
158
|
-
#
|
159
|
-
def content_type(type=nil, params={})
|
160
|
-
unless type.nil?
|
161
|
-
super(type, params)
|
162
|
-
@_content_type = type
|
163
|
-
end
|
164
|
-
@_content_type
|
165
|
-
end
|
166
|
-
|
167
|
-
private
|
168
|
-
##
|
169
|
-
# Enhancing Sinatra render functionality for:
|
170
|
-
#
|
171
|
-
# * Using layout similar to rails
|
172
|
-
# * Use render 'path/to/my/template' (without symbols)
|
173
|
-
# * Use render 'path/to/my/template' (with engine lookup)
|
174
|
-
# * Use render 'path/to/template.haml' (with explicit engine lookup)
|
175
|
-
# * Use render 'path/to/template', :layout => false
|
176
|
-
# * Use render 'path/to/template', :layout => false, :engine => 'haml'
|
177
|
-
#
|
178
|
-
def render(engine, data=nil, options={}, locals={}, &block)
|
179
|
-
|
180
|
-
# If engine is nil, ignore engine parameter and shift up all arguments
|
181
|
-
# render nil, "index", { :layout => true }, { :localvar => "foo" }
|
182
|
-
engine, data, options = data, options, locals if engine.nil? && data
|
183
|
-
|
184
|
-
# Data is a hash of options when no engine isn't explicit
|
185
|
-
# render "index", { :layout => true }, { :localvar => "foo" }
|
186
|
-
# Data is options, and options is locals in this case
|
187
|
-
data, options, locals = nil, data, options if data.is_a?(Hash)
|
188
|
-
|
189
|
-
# If data is unassigned then this is a likely a template to be resolved
|
190
|
-
# This means that no engine was explicitly defined
|
191
|
-
data, engine = *resolve_template(engine, options.dup) if data.nil?
|
192
|
-
|
193
|
-
# Use @layout if it exists
|
194
|
-
layout_was = options[:layout]
|
195
|
-
options[:layout] = @layout if options[:layout].nil? || options[:layout] == true
|
196
|
-
# Resolve layouts similar to in Rails
|
197
|
-
if options[:layout].nil? && !settings.templates.has_key?(:layout)
|
198
|
-
layout_path = settings.fetch_layout_path(options[:layout])
|
199
|
-
is_included_extension = %w[.slim .erb .haml].include?(File.extname(layout_path.to_s))
|
200
|
-
layout_path, layout_engine = *(is_included_extension ? resolve_template(layout_path) : resolved_layout)
|
201
|
-
|
202
|
-
# We need to force layout false so sinatra don't try to render it
|
203
|
-
options[:layout] = layout_path || false
|
204
|
-
options[:layout] = false unless is_included_extension ? layout_engine : layout_engine == engine
|
205
|
-
options[:layout_engine] = layout_engine || engine if options[:layout]
|
206
|
-
elsif options[:layout].present?
|
207
|
-
options[:layout], options[:layout_engine] = *resolve_template(settings.fetch_layout_path(options[:layout]), options)
|
208
|
-
end
|
209
|
-
# Default to original layout value if none found.
|
210
|
-
options[:layout] ||= layout_was
|
211
|
-
|
212
|
-
# Cleanup the template.
|
213
|
-
@current_engine, engine_was = engine, @current_engine
|
214
|
-
@_out_buf, _buf_was = ActiveSupport::SafeBuffer.new, @_out_buf
|
215
|
-
|
216
|
-
# Pass arguments to Sinatra render method.
|
217
|
-
super(engine, data, options.dup, locals, &block)
|
218
|
-
ensure
|
219
|
-
@current_engine = engine_was
|
220
|
-
@_out_buf = _buf_was
|
221
|
-
end
|
222
|
-
|
223
|
-
##
|
224
|
-
# Returns the located layout tuple to be used for the rendered template
|
225
|
-
# (if available).
|
226
|
-
#
|
227
|
-
# @example
|
228
|
-
# resolve_layout
|
229
|
-
# # => ["/layouts/custom", :erb]
|
230
|
-
# # => [nil, nil]
|
231
|
-
#
|
232
|
-
def resolved_layout
|
233
|
-
located_layout = resolve_template(settings.fetch_layout_path, :raise_exceptions => false, :strict_format => true)
|
234
|
-
located_layout ? located_layout : [nil, nil]
|
235
|
-
end
|
236
|
-
|
237
|
-
##
|
238
|
-
# Returns the template path and engine that match content_type (if present),
|
239
|
-
# I18n.locale.
|
240
|
-
#
|
241
|
-
# @param [String] template_path
|
242
|
-
# The path of the template.
|
243
|
-
#
|
244
|
-
# @param [Hash] options
|
245
|
-
# Additional options.
|
246
|
-
#
|
247
|
-
# @option options [Boolean] :strict_format (false)
|
248
|
-
# The resolved template must match the content_type of the request.
|
249
|
-
#
|
250
|
-
# @option options [Boolean] :raise_exceptions (false)
|
251
|
-
# Raises a {TemplateNotFound} exception if the template cannot be located.
|
252
|
-
#
|
253
|
-
# @return [Array<Symbol, Symbol>]
|
254
|
-
# The path and format of the template.
|
255
|
-
#
|
256
|
-
# @raise [TemplateNotFound]
|
257
|
-
# The template could not be found.
|
258
|
-
#
|
259
|
-
# @example
|
260
|
-
# get "/foo", :provides => [:html, :js] do; render 'path/to/foo'; end
|
261
|
-
# # If you request "/foo.js" with I18n.locale == :ru => [:"/path/to/foo.ru.js", :erb]
|
262
|
-
# # If you request "/foo" with I18n.locale == :de => [:"/path/to/foo.de.haml", :haml]
|
263
|
-
#
|
264
|
-
def resolve_template(template_path, options={})
|
265
|
-
began_at = Time.now
|
266
|
-
_content_type = content_type || :html
|
267
|
-
# Fetch cached template for rendering options
|
268
|
-
template_path = template_path.to_s[0] == ?/ ? template_path.to_s : "/#{template_path}"
|
269
|
-
rendering_options = [template_path, _content_type, locale]
|
270
|
-
cached_template = settings.fetch_template_file(rendering_options)
|
271
|
-
if cached_template
|
272
|
-
logger.debug :cached, began_at, cached_template[0] if settings.logging? && defined?(logger)
|
273
|
-
return cached_template
|
274
|
-
end
|
275
|
-
|
276
|
-
# Resolve view path and options.
|
277
|
-
options = DEFAULT_RENDERING_OPTIONS.merge(options)
|
278
|
-
view_path = options.delete(:views) || settings.views || "./views"
|
279
|
-
target_extension = File.extname(template_path)[1..-1] || "none" # explicit template extension
|
280
|
-
template_path = template_path.chomp(".#{target_extension}")
|
281
|
-
template_glob =
|
282
|
-
if respond_to?(:request) && request.controller.present?
|
283
|
-
File.join("{,#{request.controller}}", template_path)
|
284
|
-
else
|
285
|
-
template_path
|
286
|
-
end
|
287
|
-
|
288
|
-
# Generate potential template candidates
|
289
|
-
templates = Dir[File.join(view_path, template_glob) + ".*"].map do |file|
|
290
|
-
template_engine = File.extname(file)[1..-1].to_sym # Retrieves engine extension
|
291
|
-
template_file = file.squeeze('/').sub(view_path, '').chomp(".#{template_engine}").to_sym # retrieves template filename
|
292
|
-
[template_file, template_engine] unless IGNORE_FILE_PATTERN.any? { |pattern| template_engine.to_s =~ pattern }
|
293
|
-
end
|
294
|
-
|
295
|
-
# Check if we have a simple content type
|
296
|
-
simple_content_type = [:html, :plain].include?(_content_type)
|
297
|
-
|
298
|
-
# Resolve final template to render
|
299
|
-
located_template =
|
300
|
-
templates.find { |file, e| file.to_s == "#{template_path}.#{locale}.#{_content_type}" } ||
|
301
|
-
templates.find { |file, e| file.to_s == "#{template_path}.#{locale}" && simple_content_type } ||
|
302
|
-
templates.find { |file, e| File.extname(file.to_s) == ".#{target_extension}" or e.to_s == target_extension.to_s } ||
|
303
|
-
templates.find { |file, e| file.to_s == "#{template_path}.#{_content_type}" } ||
|
304
|
-
templates.find { |file, e| file.to_s == "#{template_path}" && simple_content_type } ||
|
305
|
-
(!options[:strict_format] && templates.first) # If not strict, fall back to the first located template.
|
306
|
-
|
307
|
-
raise TemplateNotFound, "Template '#{template_path}' not found in '#{view_path}'!" if !located_template && options[:raise_exceptions]
|
308
|
-
settings.cache_template_file!(located_template, rendering_options) unless settings.reload_templates?
|
309
|
-
logger.debug :template, began_at, located_template[0] if located_template && settings.logging? && defined?(logger)
|
310
|
-
located_template
|
311
|
-
end
|
312
|
-
|
313
|
-
##
|
314
|
-
# Return the I18n.locale if I18n is defined.
|
315
|
-
#
|
316
|
-
def locale
|
317
|
-
I18n.locale if defined?(I18n)
|
318
|
-
end
|
319
|
-
end
|
320
|
-
end
|
321
|
-
end
|
322
|
-
|
323
|
-
require 'padrino-core/application/rendering/extensions/haml'
|
324
|
-
require 'padrino-core/application/rendering/extensions/erubis'
|
325
|
-
require 'padrino-core/application/rendering/extensions/slim'
|