Am working on Rails Tutorials and I reached the layout part.
It is instructed to use “provide” in the views, and use “yield” in the layout file.
why don’t we just use instant variable instead
in code:
view about:
<% @title = 'about' %>
application layout
<title>Website Name | <%= @title %></title>
The book use instead a more complicated syntax, and am sure for a good reason
view about:
<% provide(:title, 'Help') %>
application layout
<title>Website Name | <%= yield(:title) %></title>
Tried both, and both worked fine. But I don’t understand why not use the simpler instant variables way?
Instead of provide you can use content_for and pass a block of code. Then yield will be able to execute this block of code – it doesn’t just print a variable.
This is useful because in more complex views you will use another concept know as “helpers”. Keep going with the good work and I’m sure things will become clear in the future.