Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extract the layout proc into a method, and write documentation explai…

…ning what the proc does in various cases.
  • Loading branch information...
commit b4a91db441fa9583df24fb8d3cf0d6906e8359db 1 parent ca8f8fb
Yehuda Katz + Carl Lerche authored
View
4 actionpack/lib/action_view/helpers/capture_helper.rb
@@ -111,10 +111,6 @@ def capture(*args, &block)
#
# WARNING: content_for is ignored in caches. So you shouldn't use it
# for elements that will be fragment cached.
- #
- # The deprecated way of accessing a content_for block is to use an instance variable
- # named <tt>@content_for_#{name_of_the_content_block}</tt>. The preferred usage is now
- # <tt><%= yield :footer %></tt>.
def content_for(name, content = nil, &block)
content = capture(&block) if block_given?
return @_content_for[name] << content if content
View
43 actionpack/lib/action_view/render/rendering.rb
@@ -60,16 +60,49 @@ def _render_content_with_layout(content, layout, locals)
end
end
+ # You can think of a layout as a method that is called with a block. This method
+ # returns the block that the layout is called with. If the user calls yield :some_name,
+ # the block, by default, returns content_for(:some_name). If the user calls yield,
+ # the default block returns content_for(:layout).
+ #
+ # The user can override this default by passing a block to the layout.
+ #
+ # ==== Example
+ #
+ # # The template
+ # <% render :layout => "my_layout" do %>Content<% end %>
+ #
+ # # The layout
+ # <html><% yield %></html>
+ #
+ # In this case, instead of the default block, which would return content_for(:layout),
+ # this method returns the block that was passed in to render layout, and the response
+ # would be <html>Content</html>.
+ #
+ # Finally, the block can take block arguments, which can be passed in by yield.
+ #
+ # ==== Example
+ #
+ # # The template
+ # <% render :layout => "my_layout" do |name| %>Hello <%= customer.name %><% end %>

Really exciting stuff.
Just a quick note on the example on line #87. Shouldn't the proc yield customer and not name?

<% render :layout => "my_layout" do |customer| %>Hello <%= customer.name %><% end %>

vs current:

<% render :layout => "my_layout" do |name| %>Hello <%= customer.name %><% end %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ #
+ # # The layout
+ # <html><% yield Struct.new(:name).new("David") %></html>
+ #
+ # In this case, the layout would receive the block passed into <tt>render :layout</tt>,
+ # and the Struct specified in the layout would be passed into the block. The result
+ # would be <html>Hello David</html>.
+ def layout_proc(name)
+ @_default_layout ||= proc { |*names| @_content_for[names.first || :layout] }
+ !@_content_for.key?(name) && @_proc_for_layout || @_default_layout
+ end
+
def _render_template(template, local_assigns = {})
with_template(template) do
_evaluate_assigns_and_ivars
template.render(self, local_assigns) do |*names|
- if !@_content_for.key?(names.first) && @_proc_for_layout
- capture(*names, &@_proc_for_layout)
- elsif content = @_content_for[names.first || :layout]
- content
- end
+ capture(*names, &layout_proc(names.first))
end
end
rescue Exception => e

1 comment on commit b4a91db

@kevinvaldek

Really exciting stuff.
Just a quick note on the example on line #87. Shouldn't the proc yield customer and not name?

<% render :layout => "my_layout" do |customer| %>Hello <%= customer.name %><% end %>

vs current:

<% render :layout => "my_layout" do |name| %>Hello <%= customer.name %><% end %>

@wycats
Collaborator

You are absolutely correct.

Please sign in to comment.
Something went wrong with that request. Please try again.