1
module ActionController #:nodoc:
3
def self.included(base)
4
base.extend(ClassMethods)
7
alias_method_chain :inherited, :layout
12
# Layouts reverse the common pattern of including shared headers and footers in many templates to isolate changes in
13
# repeated setups. The inclusion pattern has pages that look like this:
15
# <%= render "shared/header" %>
17
# <%= render "shared/footer" %>
19
# This approach is a decent way of keeping common structures isolated from the changing content, but it's verbose
20
# and if you ever want to change the structure of these two includes, you'll have to change all the templates.
22
# With layouts, you can flip it around and have the common structure know where to insert changing content. This means
23
# that the header and footer are only mentioned in one place, like this:
25
# // The header part of this layout
27
# // The footer part of this layout
29
# And then you have content pages that look like this:
33
# At rendering time, the content page is computed and then inserted in the layout, like this:
35
# // The header part of this layout
37
# // The footer part of this layout
39
# NOTE: The old notation for rendering the view from a layout was to expose the magic <tt>@content_for_layout</tt> instance
40
# variable. The preferred notation now is to use <tt>yield</tt>, as documented above.
42
# == Accessing shared variables
44
# Layouts have access to variables specified in the content pages and vice versa. This allows you to have layouts with
45
# references that won't materialize before rendering time:
47
# <h1><%= @page_title %></h1>
50
# ...and content pages that fulfill these references _at_ rendering time:
52
# <% @page_title = "Welcome" %>
53
# Off-world colonies offers you a chance to start a new life
55
# The result after rendering is:
58
# Off-world colonies offers you a chance to start a new life
60
# == Automatic layout assignment
62
# If there is a template in <tt>app/views/layouts/</tt> with the same name as the current controller then it will be automatically
63
# set as that controller's layout unless explicitly told otherwise. Say you have a WeblogController, for example. If a template named
64
# <tt>app/views/layouts/weblog.erb</tt> or <tt>app/views/layouts/weblog.builder</tt> exists then it will be automatically set as
65
# the layout for your WeblogController. You can create a layout with the name <tt>application.erb</tt> or <tt>application.builder</tt>
66
# and this will be set as the default controller if there is no layout with the same name as the current controller and there is
67
# no layout explicitly assigned with the +layout+ method. Nested controllers use the same folder structure for automatic layout.
68
# assignment. So an Admin::WeblogController will look for a template named <tt>app/views/layouts/admin/weblog.erb</tt>.
69
# Setting a layout explicitly will always override the automatic behaviour for the controller where the layout is set.
70
# Explicitly setting the layout in a parent class, though, will not override the child class's layout assignment if the child
71
# class has a layout with the same name.
73
# == Inheritance for layouts
75
# Layouts are shared downwards in the inheritance hierarchy, but not upwards. Examples:
77
# class BankController < ActionController::Base
78
# layout "bank_standard"
80
# class InformationController < BankController
82
# class VaultController < BankController
83
# layout :access_level_layout
85
# class EmployeeController < BankController
88
# The InformationController uses "bank_standard" inherited from the BankController, the VaultController overwrites
89
# and picks the layout dynamically, and the EmployeeController doesn't want to use a layout at all.
93
# Layouts are basically just regular templates, but the name of this template needs not be specified statically. Sometimes
94
# you want to alternate layouts depending on runtime information, such as whether someone is logged in or not. This can
95
# be done either by specifying a method reference as a symbol or using an inline method (as a proc).
97
# The method reference is the preferred approach to variable layouts and is used like this:
99
# class WeblogController < ActionController::Base
100
# layout :writers_and_readers
107
# def writers_and_readers
108
# logged_in? ? "writer_layout" : "reader_layout"
111
# Now when a new request for the index action is processed, the layout will vary depending on whether the person accessing
112
# is logged in or not.
114
# If you want to use an inline method, such as a proc, do something like this:
116
# class WeblogController < ActionController::Base
117
# layout proc{ |controller| controller.logged_in? ? "writer_layout" : "reader_layout" }
119
# Of course, the most common way of specifying a layout is still just as a plain template name:
121
# class WeblogController < ActionController::Base
122
# layout "weblog_standard"
124
# If no directory is specified for the template name, the template will by default be looked for in <tt>app/views/layouts/</tt>.
125
# Otherwise, it will be looked up relative to the template root.
127
# == Conditional layouts
129
# If you have a layout that by default is applied to all the actions of a controller, you still have the option of rendering
130
# a given action or set of actions without a layout, or restricting a layout to only a single action or a set of actions. The
131
# <tt>:only</tt> and <tt>:except</tt> options can be passed to the layout call. For example:
133
# class WeblogController < ActionController::Base
134
# layout "weblog_standard", :except => :rss
140
# This will assign "weblog_standard" as the WeblogController's layout except for the +rss+ action, which will not wrap a layout
141
# around the rendered view.
143
# Both the <tt>:only</tt> and <tt>:except</tt> condition can accept an arbitrary number of method references, so
144
# #<tt>:except => [ :rss, :text_only ]</tt> is valid, as is <tt>:except => :rss</tt>.
146
# == Using a different layout in the action render call
148
# If most of your actions use the same layout, it makes perfect sense to define a controller-wide layout as described above.
149
# Sometimes you'll have exceptions where one action wants to use a different layout than the rest of the controller.
150
# You can do this by passing a <tt>:layout</tt> option to the <tt>render</tt> call. For example:
152
# class WeblogController < ActionController::Base
153
# layout "weblog_standard"
156
# render :action => "help", :layout => "help"
160
# This will render the help action with the "help" layout instead of the controller-wide "weblog_standard" layout.
162
# If a layout is specified, all rendered actions will have their result rendered
163
# when the layout <tt>yield</tt>s. This layout can itself depend on instance variables assigned during action
164
# performance and have access to them as any normal template would.
165
def layout(template_name, conditions = {}, auto = false)
166
add_layout_conditions(conditions)
167
write_inheritable_attribute(:layout, template_name)
168
write_inheritable_attribute(:auto_layout, auto)
171
def layout_conditions #:nodoc:
172
@layout_conditions ||= read_inheritable_attribute(:layout_conditions)
175
def layout_list #:nodoc:
176
Array(view_paths).sum([]) { |path| Dir["#{path.to_str}/layouts/**/*"] }
180
def inherited_with_layout(child)
181
inherited_without_layout(child)
182
unless child.name.blank?
183
layout_match = child.name.underscore.sub(/_controller$/, '').sub(/^controllers\//, '')
184
child.layout(layout_match, {}, true) unless child.layout_list.grep(%r{layouts/#{layout_match}(\.[a-z][0-9a-z]*)+$}).empty?
188
def add_layout_conditions(conditions)
189
write_inheritable_hash(:layout_conditions, normalize_conditions(conditions))
192
def normalize_conditions(conditions)
193
conditions.inject({}) {|hash, (key, value)| hash.merge(key => [value].flatten.map {|action| action.to_s})}
197
def initialize(*args)
202
# Returns the name of the active layout. If the layout was specified as a method reference (through a symbol), this method
203
# is called and the return value is used. Likewise if the layout was specified as an inline method (through a proc or method
204
# object). If the layout was defined without a directory, layouts is assumed. So <tt>layout "weblog/standard"</tt> will return
205
# weblog/standard, but <tt>layout "standard"</tt> will return layouts/standard.
206
def active_layout(passed_layout = nil, options = {})
207
layout = passed_layout || default_layout
208
return layout if layout.respond_to?(:render)
210
active_layout = case layout
211
when Symbol then __send__(layout)
212
when Proc then layout.call(self)
216
find_layout(active_layout, default_template_format, options[:html_fallback]) if active_layout
220
def default_layout #:nodoc:
221
layout = self.class.read_inheritable_attribute(:layout)
222
return layout unless self.class.read_inheritable_attribute(:auto_layout)
223
find_layout(layout, default_template_format)
224
rescue ActionView::MissingTemplate
228
def find_layout(layout, format, html_fallback=false) #:nodoc:
229
view_paths.find_template(layout.to_s =~ /\A\/|layouts\// ? layout : "layouts/#{layout}", format, html_fallback)
230
rescue ActionView::MissingTemplate
231
raise if Mime::Type.lookup_by_extension(format.to_s).html?
234
def pick_layout(options)
235
if options.has_key?(:layout)
236
case layout = options.delete(:layout)
239
when NilClass, TrueClass
240
active_layout if action_has_layout? && candidate_for_layout?(:template => default_template_name)
242
active_layout(layout, :html_fallback => true)
245
active_layout if action_has_layout? && candidate_for_layout?(options)
249
def action_has_layout?
250
if conditions = self.class.layout_conditions
252
when only = conditions[:only]
253
only.include?(action_name)
254
when except = conditions[:except]
255
!except.include?(action_name)
264
def candidate_for_layout?(options)
265
template = options[:template] || default_template(options[:action])
266
if options.values_at(:text, :xml, :json, :file, :inline, :partial, :nothing, :update).compact.empty?
268
template_object = self.view_paths.find_template(template, default_template_format)
269
# this restores the behavior from 2.2.2, where response.template.template_format was reset
270
# to :html for :js requests with a matching html template.
271
# see v2.2.2, ActionView::Base, lines 328-330
272
@real_format = :html if response.template.template_format == :js && template_object.format == "html"
273
!template_object.exempt_from_layout?
274
rescue ActionView::MissingTemplate
278
rescue ActionView::MissingTemplate
282
def default_template_format
283
@real_format || response.template.template_format