--- !ruby/object:RI::ClassDescription 
attributes: []

class_methods: []

comment: 
- !ruby/struct:SM::Flow::P 
  body: "Layouts reverse the common pattern of including shared headers and footers in many templates to isolate changes in repeated setups. The inclusion pattern has pages that look like this:"
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;%= render &quot;shared/header&quot; %&gt;\n  Hello World\n  &lt;%= render &quot;shared/footer&quot; %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: This approach is a decent way of keeping common structures isolated from the changing content, but it's verbose and if you ever want to change the structure of these two includes, you'll have to change all the templates.
- !ruby/struct:SM::Flow::P 
  body: "With layouts, you can flip it around and have the common structure know where to insert changing content. This means that the header and footer are only mentioned in one place, like this:"
- !ruby/struct:SM::Flow::VERB 
  body: "  // The header part of this layout\n  &lt;%= yield %&gt;\n  // The footer part of this layout --&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: "And then you have content pages that look like this:"
- !ruby/struct:SM::Flow::VERB 
  body: "   hello world\n"
- !ruby/struct:SM::Flow::P 
  body: "Not a word about common structures. At rendering time, the content page is computed and then inserted in the layout, like this:"
- !ruby/struct:SM::Flow::VERB 
  body: "  // The header part of this layout\n  hello world\n  // The footer part of this layout --&gt;\n"
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Accessing shared variables
- !ruby/struct:SM::Flow::P 
  body: "Layouts have access to variables specified in the content pages and vice versa. This allows you to have layouts with references that won't materialize before rendering time:"
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;h1&gt;&lt;%= @page_title %&gt;&lt;/h1&gt;\n  &lt;%= yield %&gt;\n"
- !ruby/struct:SM::Flow::P 
  body: "...and content pages that fulfill these references <em>at</em> rendering time:"
- !ruby/struct:SM::Flow::VERB 
  body: "   &lt;% @page_title = &quot;Welcome&quot; %&gt;\n   Off-world colonies offers you a chance to start a new life\n"
- !ruby/struct:SM::Flow::P 
  body: "The result after rendering is:"
- !ruby/struct:SM::Flow::VERB 
  body: "  &lt;h1&gt;Welcome&lt;/h1&gt;\n  Off-world colonies offers you a chance to start a new life\n"
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Automatic layout assignment
- !ruby/struct:SM::Flow::P 
  body: If there is a template in <tt>app/views/layouts/</tt> with the same name as the current controller then it will be automatically set as that controller's layout unless explicitly told otherwise. Say you have a WeblogController, for example. If a template named <tt>app/views/layouts/weblog.erb</tt> or <tt>app/views/layouts/weblog.builder</tt> exists then it will be automatically set as the layout for your WeblogController. You can create a layout with the name <tt>application.erb</tt> or <tt>application.builder</tt> 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 no layout explicitly assigned with the <tt>layout</tt> method. Nested controllers use the same folder structure for automatic layout. assignment. So an Admin::WeblogController will look for a template named <tt>app/views/layouts/admin/weblog.erb</tt>. Setting a layout explicitly will always override the automatic behaviour for the controller where the layout is set. Explicitly setting the layout in a parent class, though, will not override the child class's layout assignment if the child class has a layout with the same name.
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Inheritance for layouts
- !ruby/struct:SM::Flow::P 
  body: "Layouts are shared downwards in the inheritance hierarchy, but not upwards. Examples:"
- !ruby/struct:SM::Flow::VERB 
  body: "  class BankController &lt; ActionController::Base\n    layout &quot;bank_standard&quot;\n\n  class InformationController &lt; BankController\n\n  class VaultController &lt; BankController\n    layout :access_level_layout\n\n  class EmployeeController &lt; BankController\n    layout nil\n"
- !ruby/struct:SM::Flow::P 
  body: The InformationController uses &quot;bank_standard&quot; inherited from the BankController, the VaultController overwrites and picks the layout dynamically, and the EmployeeController doesn't want to use a layout at all.
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Types of layouts
- !ruby/struct:SM::Flow::P 
  body: Layouts are basically just regular templates, but the name of this template needs not be specified statically. Sometimes you want to alternate layouts depending on runtime information, such as whether someone is logged in or not. This can be done either by specifying a method reference as a symbol or using an inline method (as a proc).
- !ruby/struct:SM::Flow::P 
  body: "The method reference is the preferred approach to variable layouts and is used like this:"
- !ruby/struct:SM::Flow::VERB 
  body: "  class WeblogController &lt; ActionController::Base\n    layout :writers_and_readers\n\n    def index\n      # fetching posts\n    end\n\n    private\n      def writers_and_readers\n        logged_in? ? &quot;writer_layout&quot; : &quot;reader_layout&quot;\n      end\n"
- !ruby/struct:SM::Flow::P 
  body: Now when a new request for the index action is processed, the layout will vary depending on whether the person accessing is logged in or not.
- !ruby/struct:SM::Flow::P 
  body: "If you want to use an inline method, such as a proc, do something like this:"
- !ruby/struct:SM::Flow::VERB 
  body: "  class WeblogController &lt; ActionController::Base\n    layout proc{ |controller| controller.logged_in? ? &quot;writer_layout&quot; : &quot;reader_layout&quot; }\n"
- !ruby/struct:SM::Flow::P 
  body: "Of course, the most common way of specifying a layout is still just as a plain template name:"
- !ruby/struct:SM::Flow::VERB 
  body: "  class WeblogController &lt; ActionController::Base\n    layout &quot;weblog_standard&quot;\n"
- !ruby/struct:SM::Flow::P 
  body: If no directory is specified for the template name, the template will by default be looked for in +app/views/layouts/+. Otherwise, it will be looked up relative to the template root.
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Conditional layouts
- !ruby/struct:SM::Flow::P 
  body: "If you have a layout that by default is applied to all the actions of a controller, you still have the option of rendering 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 <tt>:only</tt> and <tt>:except</tt> options can be passed to the layout call. For example:"
- !ruby/struct:SM::Flow::VERB 
  body: "  class WeblogController &lt; ActionController::Base\n    layout &quot;weblog_standard&quot;, :except =&gt; :rss\n\n    # ...\n\n  end\n"
- !ruby/struct:SM::Flow::P 
  body: This will assign &quot;weblog_standard&quot; as the WeblogController's layout except for the <tt>rss</tt> action, which will not wrap a layout around the rendered view.
- !ruby/struct:SM::Flow::P 
  body: "Both the <tt>:only</tt> and <tt>:except</tt> condition can accept an arbitrary number of method references, so #<tt>:except =&gt; [ :rss, :text_only ]</tt> is valid, as is <tt>:except =&gt; :rss</tt>."
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Using a different layout in the action render call
- !ruby/struct:SM::Flow::P 
  body: "If most of your actions use the same layout, it makes perfect sense to define a controller-wide layout as described above. Some times you'll have exceptions, though, where one action wants to use a different layout than the rest of the controller. This is possible using the <tt>render</tt> method. It's just a bit more manual work as you'll have to supply fully qualified template and layout names as this example shows:"
- !ruby/struct:SM::Flow::VERB 
  body: "  class WeblogController &lt; ActionController::Base\n    def help\n      render :action =&gt; &quot;help/index&quot;, :layout =&gt; &quot;help&quot;\n    end\n  end\n"
- !ruby/struct:SM::Flow::P 
  body: As you can see, you pass the template as the first parameter, the status code as the second (&quot;200&quot; is OK), and the layout as the third.
- !ruby/struct:SM::Flow::P 
  body: "NOTE: The old notation for rendering the view from a layout was to expose the magic <tt>@content_for_layout</tt> instance variable. The preferred notation now is to use <tt>yield</tt>, as documented above."
constants: []

full_name: ActionController::Layout::ClassMethods
includes: []

instance_methods: 
- !ruby/object:RI::MethodSummary 
  name: layout
name: ClassMethods
superclass: 
