Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7261

Show
Ignore:
Timestamp:
08/02/07 20:10:03 (1 year ago)
Author:
david
Message:

Added partial layouts (see example in action_view/lib/partials.rb) [DHH]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r7234 r7261  
    11*SVN* 
     2 
     3* Added partial layouts (see example in action_view/lib/partials.rb) [DHH] 
    24 
    35* Allow you to set custom :conditions on resource routes.  [Rick] 
  • trunk/actionpack/lib/action_view/base.rb

    r7034 r7261  
    305305        options = options.reverse_merge(:type => :erb, :locals => {}, :use_full_path => true) 
    306306 
    307         if options[:file] 
     307        if options[:layout] 
     308          path, partial_name = partial_pieces(options.delete(:layout)) 
     309 
     310          if block_given? 
     311            @content_for_layout = capture(&block) 
     312            concat(render(options.merge(:partial => "#{path}/#{partial_name}")), block.binding) 
     313          else 
     314            @content_for_layout = render(options) 
     315            render(options.merge(:partial => "#{path}/#{partial_name}")) 
     316          end 
     317        elsif options[:file] 
    308318          render_file(options[:file], options[:use_full_path], options[:locals]) 
    309319        elsif options[:partial] && options[:collection] 
  • trunk/actionpack/lib/action_view/partials.rb

    r7158 r7261  
    4545  # 
    4646  # This will render the partial "advertisement/_ad.erb" regardless of which controller this is being called from. 
     47  # 
     48  # == Rendering partials with layouts 
     49  # 
     50  # Partials can have their own layouts applied to them. These layouts are different than the ones that are specified globally 
     51  # for the entire action, but they work in a similar fashion. Imagine a list with two types of users: 
     52  # 
     53  #   <!-- app/views/users/index.html.erb --> 
     54  #   Here's the administrator: 
     55  #   <%= render :partial => "user", :layout => "administrator", :locals => { :user => administrator } %> 
     56  # 
     57  #   Here's the editor: 
     58  #   <%= render :partial => "user", :layout => "editor", :locals => { :user => editor } %> 
     59  # 
     60  #   <!-- app/views/users/_user.html.erb --> 
     61  #   Name: <%= user.name %> 
     62  # 
     63  #   <!-- app/views/users/_administrator.html.erb --> 
     64  #   <div id="administrator"> 
     65  #     Budget: $<%= user.budget %> 
     66  #     <%= yield %> 
     67  #   </div> 
     68  # 
     69  #   <!-- app/views/users/_editor.html.erb --> 
     70  #   <div id="editor"> 
     71  #     Deadline: $<%= user.deadline %> 
     72  #     <%= yield %> 
     73  #   </div> 
     74  # 
     75  # ...this will return: 
     76  # 
     77  #   Here's the administrator: 
     78  #   <div id="administrator"> 
     79  #     Budget: $<%= user.budget %> 
     80  #     Name: <%= user.name %> 
     81  #   </div> 
     82  # 
     83  #   Here's the editor: 
     84  #   <div id="editor"> 
     85  #     Deadline: $<%= user.deadline %> 
     86  #     Name: <%= user.name %> 
     87  #   </div> 
     88  # 
     89  # You can also apply a layout to a block within any template: 
     90  # 
     91  #   <!-- app/views/users/_chief.html.erb --> 
     92  #   <% render(:layout => "administrator", :locals => { :user => chief }) do %> 
     93  #     Title: <%= chief.title %> 
     94  #   <% end %> 
     95  # 
     96  # ...this will return: 
     97  # 
     98  #   <div id="administrator"> 
     99  #     Budget: $<%= user.budget %> 
     100  #     Title: <%= chief.name %> 
     101  #   </div> 
     102  # 
     103  # As you can see, the :locals hash is shared between both the partial and its layout. 
    47104  module Partials 
    48105    private 
  • trunk/actionpack/test/controller/new_render_test.rb

    r7190 r7261  
    348348    response.content_type = Mime::RSS 
    349349    render :text => "hello world!" 
     350  end 
     351 
     352  def render_call_to_partial_with_layout 
     353    render :action => "calling_partial_with_layout" 
     354  end 
     355 
     356  def render_using_layout_around_block 
     357    render :action => "using_layout_around_block" 
    350358  end 
    351359 
     
    794802    assert_equal "http://www.nextangle.com/customers/1", @response.headers["Location"] 
    795803  end 
     804 
     805  def test_render_call_to_partial_with_layout 
     806    get :render_call_to_partial_with_layout 
     807    assert_equal "Before (David)\nInside from partial (David)\nAfter", @response.body 
     808  end 
     809   
     810  def test_using_layout_around_block 
     811    get :using_layout_around_block 
     812    assert_equal "Before (David)\nInside from block\nAfter", @response.body 
     813  end 
    796814end