嵌入式 Ruby——渲染与产出? [英] Embedded Ruby -- Render vs. Yield?

查看:39
本文介绍了嵌入式 Ruby——渲染与产出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解 html.erb 文件是如何工作的,我对渲染和产量命令有点困惑,因为它们似乎都是通过替换其他 html 中的代码来使文件管理器更干净和更简单的方法.erb 文件.有人可以向我解释渲染和产量之间的区别吗?

I'm trying to understand how html.erb files work, and I am a little confused about the render and yield commands, as they both seem to be ways to make the filer cleaner and simpler by substituting in code from other html.erb files. Can someone explain to me the difference between render and yield?

推荐答案

render 用于调用部分页面模板,而 yield 用于您想要的占位符模板的输出以产生其内容.所以你在构建内容的时候使用render,yield来展示内容的本质.

render is used for invoking a partial page template whereas yield is used a placeholder where you want the output of your templates to yield their content. So you use render when building up the content, and yield to show the content in essence.

作为一般经验法则,yield 用于布局"级别模板(在最基本的示例中,/app/views/layout 目录中的 application.html.erb).Render 用于您的资源/操作特定模板.

As a general rule of thumb, yield is used in the 'layout' level templates (in the most basic example, the application.html.erb in the /app/views/layout directory). Render is used in your resource/action specific templates.

另请查看 content_for 标记(块)以及如何使用它进一步将应用程序级模板分解为多个部分.

Also take a look at the content_for tag (block) and how you can use it to further break up your application-level templates into sections.

强制性指南@rubyonrails.org 链接:http://guides.rubyonrails.org/layouts_and_rendering.html

Obligatory guides@rubyonrails.org link: http://guides.rubyonrails.org/layouts_and_rendering.html

这篇关于嵌入式 Ruby——渲染与产出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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