Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
c82a9193
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
c82a9193
编写于
3月 13, 2020
作者:
J
Joel Hawksley
提交者:
GitHub
3月 13, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Document view components support (#38656)
上级
99b607e6
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
32 addition
and
78 deletion
+32
-78
actionpack/lib/action_controller/renderer.rb
actionpack/lib/action_controller/renderer.rb
+6
-2
actionpack/test/controller/renderer_test.rb
actionpack/test/controller/renderer_test.rb
+2
-2
actionpack/test/lib/test_component.rb
actionpack/test/lib/test_component.rb
+3
-33
actionview/lib/action_view/helpers/rendering_helper.rb
actionview/lib/action_view/helpers/rendering_helper.rb
+6
-2
actionview/test/lib/test_component.rb
actionview/test/lib/test_component.rb
+3
-37
actionview/test/template/render_test.rb
actionview/test/template/render_test.rb
+2
-2
guides/source/layouts_and_rendering.md
guides/source/layouts_and_rendering.md
+10
-0
未找到文件。
actionpack/lib/action_controller/renderer.rb
浏览文件 @
c82a9193
...
...
@@ -82,8 +82,12 @@ def initialize(controller, env, defaults)
# need to call <tt>.to_json</tt> on the object you want to render.
# * <tt>:body</tt> - Renders provided text and sets content type of <tt>text/plain</tt>.
#
# If no <tt>options</tt> hash is passed or if <tt>:update</tt> is specified, the default is
# to render a partial and use the second parameter as the locals hash.
# If no <tt>options</tt> hash is passed or if <tt>:update</tt> is specified, then:
#
# If an object responding to `render_in` is passed, `render_in` is called on the object,
# passing in the current view context.
#
# Otherwise, a partial is rendered using the second parameter as the locals hash.
def
render
(
*
args
)
raise
"missing controller"
unless
controller
...
...
actionpack/test/controller/renderer_test.rb
浏览文件 @
c82a9193
...
...
@@ -70,8 +70,8 @@ def test_render_component
renderer
=
ApplicationController
.
renderer
assert_equal
(
%(
<span title="my title">(Inline render)</span>
)
,
renderer
.
render
(
TestComponent
.
new
(
title:
"my title"
)).
strip
%(
Hello, World!
)
,
renderer
.
render
(
TestComponent
.
new
)
)
end
...
...
actionpack/test/lib/test_component.rb
浏览文件 @
c82a9193
# frozen_string_literal: true
class
TestComponent
<
ActionView
::
Base
delegate
:render
,
to: :view_context
def
initialize
(
title
:)
@title
=
title
end
def
render_in
(
view_context
)
self
.
class
.
compile
@view_context
=
view_context
rendered_template
class
TestComponent
def
render_in
(
_view_context
)
"Hello, World!"
end
def
format
:html
end
def
self
.
template
<<~
'erb'
<span title="<%= title %>">(<%= render(plain: "Inline render") %>)</span>
erb
end
def
self
.
compile
@compiled
||=
nil
return
if
@compiled
class_eval
(
"def rendered_template; @output_buffer = ActionView::OutputBuffer.new; "
+
ActionView
::
Template
::
Handlers
::
ERB
.
erb_implementation
.
new
(
template
,
trim:
true
).
src
+
"; end"
)
@compiled
=
true
end
private
attr_reader
:title
,
:view_context
end
actionview/lib/action_view/helpers/rendering_helper.rb
浏览文件 @
c82a9193
...
...
@@ -22,8 +22,12 @@ module RenderingHelper
# type of <tt>text/plain</tt> from <tt>ActionDispatch::Response</tt>
# object.
#
# If no options hash is passed or :update specified, the default is to render a partial and use the second parameter
# as the locals hash.
# If no <tt>options</tt> hash is passed or if <tt>:update</tt> is specified, then:
#
# If an object responding to `render_in` is passed, `render_in` is called on the object,
# passing in the current view context.
#
# Otherwise, a partial is rendered using the second parameter as the locals hash.
def
render
(
options
=
{},
locals
=
{},
&
block
)
case
options
when
Hash
...
...
actionview/test/lib/test_component.rb
浏览文件 @
c82a9193
# frozen_string_literal: true
class
TestComponent
<
ActionView
::
Base
delegate
:render
,
to: :view_context
def
initialize
(
title
:)
@title
=
title
end
# Entrypoint for rendering. Called by ActionView::RenderingHelper#render.
#
# Returns ActionView::OutputBuffer.
def
render_in
(
view_context
,
&
block
)
self
.
class
.
compile
@view_context
=
view_context
@content
=
view_context
.
capture
(
&
block
)
if
block_given?
rendered_template
end
def
self
.
template
<<~
'erb'
<span title="<%= title %>"><%= content %> (<%= render(plain: "Inline render") %>)</span>
erb
class
TestComponent
def
render_in
(
_view_context
)
"Hello, World!"
end
def
self
.
compile
@compiled
||=
nil
return
if
@compiled
class_eval
(
"def rendered_template; @output_buffer = ActionView::OutputBuffer.new; "
+
ActionView
::
Template
::
Handlers
::
ERB
.
erb_implementation
.
new
(
template
,
trim:
true
).
src
+
"; end"
)
@compiled
=
true
end
private
attr_reader
:content
,
:title
,
:view_context
end
actionview/test/template/render_test.rb
浏览文件 @
c82a9193
...
...
@@ -681,8 +681,8 @@ def test_render_throws_exception_when_no_extensions_passed_to_register_template_
def
test_render_component
assert_equal
(
%(
<span title="my title">Hello, World! (Inline render)</span>
)
,
@view
.
render
(
TestComponent
.
new
(
title:
"my title"
))
{
"Hello, World!"
}.
strip
%(
Hello, World!
)
,
@view
.
render
(
TestComponent
.
new
)
)
end
end
...
...
guides/source/layouts_and_rendering.md
浏览文件 @
c82a9193
...
...
@@ -277,6 +277,16 @@ since an attacker could use this action to access security sensitive files in yo
TIP:
`send_file`
is often a faster and better option if a layout isn't required.
#### Rendering objects
Rails can render objects responding to
`:render_in`
.
```
ruby
render
MyComponent
.
new
```
This calls
`render_in`
on the provided object with the current view context.
#### Options for `render`
Calls to the
`render`
method generally accept six options:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录