Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded Ruby -- Render vs. Yield?

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?

like image 944
Kvass Avatar asked Jun 09 '11 22:06

Kvass


1 Answers

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.

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.

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.

Obligatory [email protected] link: http://guides.rubyonrails.org/layouts_and_rendering.html

like image 186
colinross Avatar answered Nov 14 '22 09:11

colinross