Reputation: 5931
Could anyone give clear explanation on how provide()
works inside the view ? I have read official documentation but what really bothers me is this, if I define in the beginning of a template
<% provide(:title, 'Help') %>
and then later I have this line of code
<%= yield :title %>
what really happens in the background ? I know that yield is supposed to call code block. What would be code block in this context?
Upvotes: 24
Views: 17429
Reputation: 3229
provide
stores a block of markup in an identifier for later use. In this case, 'Help' in the symbol :title. The provide is enclosed in <% %>
to indicate it is executing this code and not printing out in the view.
yield
in this case just spits that block back out. The yield is enclosed in <%= %>
to indicate it is being printed out into the view.
Think of it as setting a variable and printing out a variable.
See: http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-provide
for more information. Note that provide
is really a wrapper for content_for
so that's where the good stuff is in that link.
Upvotes: 49