Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are erb layouts implemented?

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 )

like image 363
Geo Avatar asked Oct 25 '22 11:10

Geo


1 Answers

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

<%= yield %>

which will get replaced with the shot.html.erb text

This is also how the following work. In your page, you might say:

<% content_for(:footer) do %>
  ...
<% end %>

And then layer in your layout you can:

<%= yield(:footer) %>

I like to think of it as just an inside-out setting of variables.

like image 135
Jesse Wolgamott Avatar answered Oct 27 '22 11:10

Jesse Wolgamott