Methods
Included Modules
- AbstractController::Layouts::ClassMethods START:includes
Classes and Modules
Instance Public methods
If no layout is supplied, look for a template named the return value of this method.
Returns
- String - A template name
Creates a _layout method to be called by _default_layout .
If a layout is not explicitly mentioned then look for a layout with the controller’s name. if nothing is found then try same procedure to find super class’s layout.
# File actionpack/lib/abstract_controller/layouts.rb, line 274 274: def _write_layout_method 275: remove_possible_method(:_layout) 276: 277: prefixes = _implied_layout_name =~ /\blayouts/ ? [] : ["layouts"] 278: name_clause = if name 279: "lookup_context.find_all(\"\#{_implied_layout_name}\", \#{prefixes.inspect}).first || super\n" 280: end 281: 282: if defined?(@_layout) 283: layout_definition = case @_layout 284: when String 285: @_layout.inspect 286: when Symbol 287: "\#{@_layout}.tap do |layout|\nunless layout.is_a?(String) || !layout\nraise ArgumentError, \"Your layout method :\#{@_layout} returned \\\#{layout}. It \" \\\n\"should have returned a String, false, or nil\"\nend\nend\n" 288: when Proc 289: define_method :_layout_from_proc, &@_layout 290: "_layout_from_proc(self)" 291: when false 292: nil 293: when true 294: raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil" 295: when nil 296: name_clause 297: end 298: else 299: # Add a deprecation if the parent layout was explicitly set and the child 300: # still does a dynamic lookup. In next Rails release, we should @_layout 301: # to be inheritable so we can skip the child lookup if the parent explicitly 302: # set the layout. 303: parent = self.superclass.instance_variable_get(:@_layout) 304: @_layout = nil 305: inspect = parent.is_a?(Proc) ? parent.inspect : parent 306: 307: layout_definition = if parent.nil? 308: name_clause 309: elsif name 310: "if template = lookup_context.find_all(\"\#{_implied_layout_name}\", \#{prefixes.inspect}).first\nActiveSupport::Deprecation.warn 'Layout found at \"\#{_implied_layout_name}\" for \#{name} but parent controller ' \\\n'set layout to \#{inspect.inspect}. Please explicitly set your layout to \"\#{_implied_layout_name}\" ' \\\n'or set it to nil to force a dynamic lookup.'\ntemplate\nelse\nsuper\nend\n" 311: end 312: end 313: 314: self.class_eval "def _layout\nif conditional_layout?\n\#{layout_definition}\nelse\n\#{name_clause}\nend\nend\nprivate :_layout\n", __FILE__, __LINE__ + 1 315: end
Specify the layout to use for this class.
If the specified layout is a:
String: | the String is the template name |
Symbol: | call the method specified by the symbol, which will return the template name |
false: | There is no layout |
true: | raise an ArgumentError |
nil: | Force default layout behavior with inheritance |
Parameters
- layout - The layout to use.
Options (conditions)
- :only - A list of actions to apply this layout to.
- :except - Apply this layout to all actions but this one.
# File actionpack/lib/abstract_controller/layouts.rb, line 251 251: def layout(layout, conditions = {}) 252: include LayoutConditions unless conditions.empty? 253: 254: conditions.each {|k, v| conditions[k] = Array(v).map {|a| a.to_s} } 255: self._layout_conditions = conditions 256: 257: @_layout = layout 258: _write_layout_method 259: end