Action ViewRenderingHelpers¶↑
Implements methods that allow rendering from a view context. In order to use this module, all you need is to implement view_renderer that returns anActionView::Renderer object.
Instance Public methods
_layout_for(*args, &block)Link
Overrides_layout_for in the context object so it supports the case a block is passed to a partial. Returns the contents that are yielded to a layout, given a name or a block.
You can think of a layout as a method that is called with a block. If the user callsyield :some_name, the block, by default, returnscontent_for(:some_name). If the user calls simplyyield, the default block returnscontent_for(:layout).
The user can override this default by passing a block to the layout:
# The template<%= render layout: "my_layout" do %> Content<% end %># The layout<html> <%= yield %></html>
In this case, instead of the default block, which would returncontent_for(:layout), this method returns the block that was passed in torender :layout, and the response would be
<html> Content</html>
Finally, the block can take block arguments, which can be passed in byyield:
# The template<%= render layout: "my_layout" do |customer| %> Hello <%= customer.name %><% end %># The layout<html> <%= yield Struct.new(:name).new("David") %></html>In this case, the layout would receive the block passed intorender :layout, and the struct specified would be passed into the block as an argument. The result would be
<html> Hello David</html>
render(options = {}, locals = {}, &block)Link
Renders a template and returns the result.
Pass the template to render as the first argument. This is shorthand syntax for partial rendering, so the template filename should be prefixed with an underscore. The partial renderer looks for the partial template in the directory of the calling template first.
<% # app/views/posts/new.html.erb %><%= render "form" %># => renders app/views/posts/_form.html.erb
Use the complete view path to render a partial from another directory.
<% # app/views/posts/show.html.erb %><%= render "comments/form" %># => renders app/views/comments/_form.html.erb
Without the rendering mode, the second argument can be aHash of local variable assignments for the template.
<% # app/views/posts/new.html.erb %><%= render "form", post: Post.new %># => renders app/views/posts/_form.html.erb
If the first argument responds torender_in, the template will be rendered by callingrender_in with the current view context.
class Greeting def render_in(view_context) view_context.render html: "<h1>Hello, World</h1>" end def format :html endend<%= render Greeting.new %># => "<h1>Hello, World</h1>"
Rendering Mode¶↑
Pass the rendering mode as first argument to override it.
:partialSee
ActionView::PartialRendererfor details.
<%= render partial: "form", locals: { post: Post.new } %> # => renders app/views/posts/_form.html.erb:fileRenders the contents of a file. This option shouldnot be used with unsanitized user input.
<%= render file: "/path/to/some/file" %> # => renders /path/to/some/file
:inlineRenders an
ERBtemplate string.
<% name = "World" %> <%= render inline: "<h1>Hello, <%= name %>!</h1>" %> # => renders "<h1>Hello, World!</h1>"
:bodyRenders the provided text, and sets the format as
:text.
<%= render body: "Hello, World!" %> # => renders "Hello, World!"
:plainRenders the provided text, and sets the format as
:text.
<%= render plain: "Hello, World!" %> # => renders "Hello, World!"
:htmlRenders the provided HTML string, and sets the format as
:html. If the string is nothtml_safe?, performs HTML escaping on the string before rendering.
<%= render html: "<h1>Hello, World!</h1>".html_safe %> # => renders "<h1>Hello, World!</h1>" <%= render html: "<h1>Hello, World!</h1>" %> # => renders "<h1>Hello, World!</h1>"
:renderableRenders the provided object by calling
render_inwith the current view context. The format is determined by callingformaton the renderable if it responds toformat, falling back to:htmlby default.
<%= render renderable: Greeting.new %> # => renders "<h1>Hello, World</h1>"
Options¶↑
:localsHashof local variable assignments for the template.
<%= render inline: "<h1>Hello, <%= name %>!</h1>", locals: { name: "World" } %> # => renders "<h1>Hello, World!</h1>":formatsOverride the current format to render a template for a different format.
<% # app/views/posts/show.html.erb %> <%= render template: "posts/content", formats: [:text] %> # => renders app/views/posts/content.text.erb
:variantsRender a template for a different variant.
<% # app/views/posts/show.html.erb %> <%= render template: "posts/content", variants: [:tablet] %> # => renders app/views/posts/content.html+tablet.erb
:handlersRender a template for a different handler.
<% # app/views/posts/show.html.erb %> <%= render template: "posts/content", handlers: [:builder] %> # => renders app/views/posts/content.html.builder
# File actionview/lib/action_view/helpers/rendering_helper.rb, line 138defrender(options = {},locals = {},&block)caseoptionswhenHashin_rendering_context(options)do|renderer|ifblock_given?view_renderer.render_partial(self,options.merge(partial:options[:layout]),&block)elseview_renderer.render(self,options)endendelseifoptions.respond_to?(:render_in)options.render_in(self,&block)elseview_renderer.render_partial(self,partial:options,locals:locals,&block)endendend