模板中的 yield 和 provide() [英] yield and provide() inside template

查看:34
本文介绍了模板中的 yield 和 provide()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以清楚地解释 provide() 在视图中的工作原理吗?我已经阅读了官方文档,但真正困扰我的是这个,如果我在模板的开头定义

Could anyone give clear explanation on how provide() works inside the view ? I have read official documentation but what really bothers me is this, if I define in the beginning of a template

<% provide(:title, 'Help') %>

然后我有这行代码

<%= yield :title %> 

后台到底发生了什么?我知道 yield 应该调用代码块.在这种情况下,代码块是什么?

what really happens in the background ? I know that yield is supposed to call code block. What would be code block in this context?

推荐答案

provide 将标记块存储在标识符中以备后用.在这种情况下,符号 :title 中的帮助".提供包含在 <% %> 中,表示它正在执行此代码而不是在视图中打印出来.

provide stores a block of markup in an identifier for later use. In this case, 'Help' in the symbol :title. The provide is enclosed in <% %> to indicate it is executing this code and not printing out in the view.

yield 在这种情况下只是将那个块吐出来.产量包含在 <%= %> 中,表示它正在打印到视图中.

yield in this case just spits that block back out. The yield is enclosed in <%= %> to indicate it is being printed out into the view.

把它想象成设置一个变量并打印出一个变量.

Think of it as setting a variable and printing out a variable.

请参阅:http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-provide想要查询更多的信息.请注意,provide 实际上是 content_for 的包装器,因此该链接中的好东西就在那里.

See: http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-provide for more information. Note that provide is really a wrapper for content_for so that's where the good stuff is in that link.

这篇关于模板中的 yield 和 provide()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆