Skip to content

Design: Layouts

William Newbery edited this page Sep 15, 2016 · 2 revisions

Layouts using content_for and yield are primarily implemented by those methods in ViewModel.

  1. When using Template::render_layout(layout, model, doctype), first the template renders itself via a normal Template::render(model, false).
  2. Any blocks passed to =content_for executed by the view model instead of returning the rendered HTML from the block (as would say content_tag), instead stores it inside the view model in a map keyed by the contents name and returns nil (which is rendered as an empty string).
  3. The HTML returned by render is then stored in the view model via ViewModel::set_main_content.
  4. Template::render_layout then calls layout.render to render the layout.
  5. When the layout calls the yield function, if it is without a name then the HTML stored by ViewModel::set_main_content is returned, or if its called with a symbol, the HTML stored by content_for if an entry with that name exists, else an empty string.
  6. Template::render_layout then returns the HTML rendered by the layout.

Clone this wiki locally