Skip to content

Instantly share code, notes, and snippets.

@jennceng
Last active November 29, 2016 20:28
Show Gist options
  • Save jennceng/ae13ca76c8ecd7c5f2b4c67fb2ad3af4 to your computer and use it in GitHub Desktop.
Save jennceng/ae13ca76c8ecd7c5f2b4c67fb2ad3af4 to your computer and use it in GitHub Desktop.
# layout.erb
<html>
<head>
  <title>My Site</title>
</head>
<body>
  <%= yield %>
</body>
</html>
# index.erb
get "/tasks" do
  @tasks = ["learn Ruby", "learn JavaScript"]

  erb :index
end
# new.erb
get "/tasks/new" do
  ...
  erb :new
end 

ANY TIME you render a template, ie index.erb, new.erb, etc.

FIRST: the layout.erb is rendered until the yield is hit (if there is a layout.erb)

SECOND: the template you specified is rendered, for example new.erb

THIRD: you return to the layout.erb and finish loading any HTML

BUT WHY?

  • The layout allows you to have things that are common to all pages

  • for example, before the yield you might have a sweet top bar and search field you want on all pages

  • you might want a dope footer after the yield that will be loaded to every page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment