erb 布局是如何实现的?
Rails ERB 的布局是如何实现的?我尝试查看源代码,但无法确定它们在哪里/如何工作。
我特别感兴趣的是 yield 部分如何工作,erb 如何在模板中包含渲染视图。
我需要它的原因是我可以使用它来生成代码,非 Rails,非 HTML 相关(并且因为了解它们如何工作会很有趣)
How are Rails ERB's layouts implemented? I tried looking through the source, but I couldn't determine where/how they work.
I am specifically interested in how the yield part works, how erb includes the rendered view in a template.
The reason I need it, is so that I can use it for code generation, non Rails, non HTML related ( and because it would be interesting to know how they work )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Rails 渲染由内而外,因此它会首先渲染 show.html.erb 并将其存储在变量中。 渲染布局
然后,它将在您看到的布局内
,该布局将被 shot.html.erb 文本替换
这也是以下工作的方式。在您的页面中,您可能会说:
然后您可以在布局中分层:
我喜欢将其视为变量的由内而外的设置。
Rails renders inside out, so it will render the show.html.erb first and store that in a variable. It will then render the layout
inside the layout you see
which will get replaced with the shot.html.erb text
This is also how the following work. In your page, you might say:
And then layer in your layout you can:
I like to think of it as just an inside-out setting of variables.