简体   繁体   中英

content_for with <% or <%=?

Simple question here. Should content for be:

<%= content_for :xyz do %>
or
<% content_for :xyz do %>

The Rails docs are ambiguous on this!

According to documentation example(http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html) <%= content_for :xyz%> is used to emit stored block of markup, while <% content_for :xyz%> is used to declare stored block of markup.

So it should look like this(eg app/views/home/index.html.erb fragment):

<% content_for :my_block do %>
    <h1>I'm header</h1>
<% end %>

And somewhere in another template(eg app/views/layouts/application.html.erb fragment):

<%= content_for :my_block %>

它应该是<%= content_for :xyz do %>

You can think of <%= %> as an insert and <% %> as computation

So in an erb file, you might do something like :

<% follower_count = current_user.followers.count %>
<%= follower_count %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM