Skip to content

Commit b408864

Browse files
authored
Merge pull request rails#52520 from p8/document-render-formats-option
Improve documentation of `ActionView:RenderingHelper#render`
2 parents 1aa85fd + bb736cf commit b408864

File tree

1 file changed

+160
-50
lines changed

1 file changed

+160
-50
lines changed

actionview/lib/action_view/helpers/rendering_helper.rb

+160-50
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,140 @@
11
# frozen_string_literal: true
22

3+
# :markup: markdown
4+
35
module ActionView
46
module Helpers # :nodoc:
5-
# = Action View \Rendering \Helpers
7+
# # Action View Rendering Helpers
68
#
7-
# Implements methods that allow rendering from a view context.
8-
# In order to use this module, all you need is to implement
9-
# view_renderer that returns an ActionView::Renderer object.
9+
# Implements methods that allow rendering from a view context. In order to use
10+
# this module, all you need is to implement view_renderer that returns an
11+
# ActionView::Renderer object.
1012
module RenderingHelper
11-
# Returns the result of a render that's dictated by the options hash. The primary options are:
13+
# Renders a template and returns the result.
14+
#
15+
# Pass the template to render as the first argument. This is shorthand
16+
# syntax for partial rendering, so the template filename should be
17+
# prefixed with an underscore. The partial renderer looks for the partial
18+
# template in the directory of the calling template first.
19+
#
20+
# <% # app/views/posts/new.html.erb %>
21+
# <%= render "form" %>
22+
# # => renders app/views/posts/_form.html.erb
23+
#
24+
# Use the complete view path to render a partial from another directory.
25+
#
26+
# <% # app/views/posts/show.html.erb %>
27+
# <%= render "comments/form" %>
28+
# # => renders app/views/comments/_form.html.erb
29+
#
30+
# Without the rendering mode, the second argument can be a Hash of local
31+
# variable assignments for the template.
32+
#
33+
# <% # app/views/posts/new.html.erb %>
34+
# <%= render "form", post: Post.new %>
35+
# # => renders app/views/posts/_form.html.erb
36+
#
37+
# If the first argument responds to `render_in`, the template will be rendered
38+
# by calling `render_in` with the current view context.
39+
#
40+
# class Greeting
41+
# def render_in(view_context)
42+
# view_context.render html: "<h1>Hello, World</h1>"
43+
# end
44+
#
45+
# def format
46+
# :html
47+
# end
48+
# end
49+
#
50+
# <%= render Greeting.new %>
51+
# # => "<h1>Hello, World</h1>"
52+
#
53+
# #### Rendering Mode
54+
#
55+
# Pass the rendering mode as first argument to override it.
56+
#
57+
# `:partial`
58+
# : See ActionView::PartialRenderer for details.
59+
#
60+
# <%= render partial: "form", locals: { post: Post.new } %>
61+
# # => renders app/views/posts/_form.html.erb
62+
#
63+
# `:file`
64+
# : Renders the contents of a file. This option should **not** be used with
65+
# unsanitized user input.
66+
#
67+
# <%= render file: "/path/to/some/file" %>
68+
# # => renders /path/to/some/file
69+
#
70+
# `:inline`
71+
# : Renders an ERB template string.
72+
#
73+
# <% name = "World" %>
74+
# <%= render inline: "<h1>Hello, <%= name %>!</h1>" %>
75+
# # => renders "<h1>Hello, World!</h1>"
76+
#
77+
# `:body`
78+
# : Renders the provided text, and sets the format as `:text`.
79+
#
80+
# <%= render body: "Hello, World!" %>
81+
# # => renders "Hello, World!"
82+
#
83+
# `:plain`
84+
# : Renders the provided text, and sets the format as `:text`.
85+
#
86+
# <%= render plain: "Hello, World!" %>
87+
# # => renders "Hello, World!"
88+
#
89+
# `:html`
90+
# : Renders the provided HTML string, and sets the format as
91+
# `:html`. If the string is not `html_safe?`, performs HTML escaping on
92+
# the string before rendering.
93+
#
94+
# <%= render html: "<h1>Hello, World!</h1>".html_safe %>
95+
# # => renders "<h1>Hello, World!</h1>"
96+
#
97+
# <%= render html: "<h1>Hello, World!</h1>" %>
98+
# # => renders "&lt;h1&gt;Hello, World!&lt;/h1&gt;"
99+
#
100+
# `:renderable`
101+
# : Renders the provided object by calling `render_in` with the current view
102+
# context. The format is determined by calling `format` on the
103+
# renderable if it responds to `format`, falling back to `:html` by
104+
# default.
105+
#
106+
# <%= render renderable: Greeting.new %>
107+
# # => renders "<h1>Hello, World</h1>"
108+
#
109+
#
110+
# #### Options
111+
#
112+
# `:locals`
113+
# : Hash of local variable assignments for the template.
114+
#
115+
# <%= render inline: "<h1>Hello, <%= name %>!</h1>", locals: { name: "World" } %>
116+
# # => renders "<h1>Hello, World!</h1>"
117+
#
118+
# `:formats`
119+
# : Override the current format to render a template for a different format.
120+
#
121+
# <% # app/views/posts/show.html.erb %>
122+
# <%= render template: "posts/content", formats: [:text] %>
123+
# # => renders app/views/posts/content.text.erb
12124
#
13-
# * <tt>:partial</tt> - See ActionView::PartialRenderer.
14-
# * <tt>:file</tt> - Renders an explicit template file (this used to be the old default), add +:locals+ to pass in those.
15-
# * <tt>:inline</tt> - Renders an inline template similar to how it's done in the controller.
16-
# * <tt>:plain</tt> - Renders the text passed in out. Setting the content
17-
# type as <tt>text/plain</tt>.
18-
# * <tt>:html</tt> - Renders the HTML safe string passed in out, otherwise
19-
# performs HTML escape on the string first. Setting the content type as
20-
# <tt>text/html</tt>.
21-
# * <tt>:body</tt> - Renders the text passed in, and inherits the content
22-
# type of <tt>text/plain</tt> from ActionDispatch::Response object.
125+
# `:variants`
126+
# : Render a template for a different variant.
23127
#
24-
# If no <tt>options</tt> hash is passed or if <tt>:update</tt> is specified, then:
128+
# <% # app/views/posts/show.html.erb %>
129+
# <%= render template: "posts/content", variants: [:tablet] %>
130+
# # => renders app/views/posts/content.html+tablet.erb
25131
#
26-
# If an object responding to +render_in+ is passed, +render_in+ is called on the object,
27-
# passing in the current view context.
132+
# `:handlers`
133+
# : Render a template for a different handler.
28134
#
29-
# Otherwise, a partial is rendered using the second parameter as the locals hash.
135+
# <% # app/views/posts/show.html.erb %>
136+
# <%= render template: "posts/content", handlers: [:builder] %>
137+
# # => renders app/views/posts/content.html.builder
30138
def render(options = {}, locals = {}, &block)
31139
case options
32140
when Hash
@@ -47,52 +155,54 @@ def render(options = {}, locals = {}, &block)
47155
end
48156

49157
# Overrides _layout_for in the context object so it supports the case a block is
50-
# passed to a partial. Returns the contents that are yielded to a layout, given a
51-
# name or a block.
158+
# passed to a partial. Returns the contents that are yielded to a layout, given
159+
# a name or a block.
52160
#
53-
# You can think of a layout as a method that is called with a block. If the user calls
54-
# <tt>yield :some_name</tt>, the block, by default, returns <tt>content_for(:some_name)</tt>.
55-
# If the user calls simply +yield+, the default block returns <tt>content_for(:layout)</tt>.
161+
# You can think of a layout as a method that is called with a block. If the user
162+
# calls `yield :some_name`, the block, by default, returns
163+
# `content_for(:some_name)`. If the user calls simply `yield`, the default block
164+
# returns `content_for(:layout)`.
56165
#
57166
# The user can override this default by passing a block to the layout:
58167
#
59-
# # The template
60-
# <%= render layout: "my_layout" do %>
61-
# Content
62-
# <% end %>
168+
# # The template
169+
# <%= render layout: "my_layout" do %>
170+
# Content
171+
# <% end %>
63172
#
64-
# # The layout
65-
# <html>
66-
# <%= yield %>
67-
# </html>
173+
# # The layout
174+
# <html>
175+
# <%= yield %>
176+
# </html>
68177
#
69-
# In this case, instead of the default block, which would return <tt>content_for(:layout)</tt>,
70-
# this method returns the block that was passed in to <tt>render :layout</tt>, and the response
178+
# In this case, instead of the default block, which would return `content_for(:layout)`,
179+
# this method returns the block that was passed in to `render :layout`, and the response
71180
# would be
72181
#
73-
# <html>
74-
# Content
75-
# </html>
182+
# <html>
183+
# Content
184+
# </html>
76185
#
77-
# Finally, the block can take block arguments, which can be passed in by +yield+:
186+
# Finally, the block can take block arguments, which can be passed in by
187+
# `yield`:
78188
#
79-
# # The template
80-
# <%= render layout: "my_layout" do |customer| %>
81-
# Hello <%= customer.name %>
82-
# <% end %>
189+
# # The template
190+
# <%= render layout: "my_layout" do |customer| %>
191+
# Hello <%= customer.name %>
192+
# <% end %>
83193
#
84-
# # The layout
85-
# <html>
86-
# <%= yield Struct.new(:name).new("David") %>
87-
# </html>
194+
# # The layout
195+
# <html>
196+
# <%= yield Struct.new(:name).new("David") %>
197+
# </html>
88198
#
89-
# In this case, the layout would receive the block passed into <tt>render :layout</tt>,
199+
# In this case, the layout would receive the block passed into `render :layout`,
90200
# and the struct specified would be passed into the block as an argument. The result
91201
# would be
92202
#
93-
# <html>
94-
# Hello David
95-
# </html>
203+
# <html>
204+
# Hello David
205+
# </html>
96206
#
97207
def _layout_for(*args, &block)
98208
name = args.first

0 commit comments

Comments
 (0)