Skip to content

Instantly share code, notes, and snippets.

@bf4
Created December 24, 2012 17:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4/4370117 to your computer and use it in GitHub Desktop.
Save bf4/4370117 to your computer and use it in GitHub Desktop.
Rails template / layout inheritance
-content_for :title do
Child Title
The rest of the page goes here
- content_for :site_name do
= 'Child Layout'
- content_for :stylesheet do
= stylesheet_link_tag 'child'
- content_for :site_header do
= render :partial => 'child/header'
= render :template => 'layouts/parent_layout'
-# this template is inherited by other layouts, like child_layout
-# and is customized by the content_for blocks there
-# that we handle there (they won't be handled on subsequent calls)
-# unless we did something like this there to check if there is a receiving content_for?
-# otherwise, in this case, yield to the view
-# = content_for?(:stylesheet) ? yield(:stylesheet) : yield
-# see http://guides.rubyonrails.org/layouts_and_rendering.html#using-nested-layouts
!!! Strict
%html{:xmlns=>"http://www.w3.org/1999/xhtml",:'xml:lang'=>"en", :lang=>"en", 'xmlns:fb'=>'http://ogp.me/ns/fb#'}
%head{:prefix => "fb: http://ogp.me/ns/fb#"}
- if content_for?(:title)
- title = yield(:title)
-else
- title = 'Welcome'
%title= "#{yield(:site_name)} - #{ title }"
= yield(:stylesheet)
%body
%div#container
%div#hd
= yield(:site_header)
%div#bd
%div#main
%div.b
%div.wrapper
-unless notice.blank?
%p.notice= notice
= yield
=yield(:javascript)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment