问题
In rails 3.0 with HAML (3.1.4) I have
some template-like partial, like _template.html.haml:
.panel.top = yield :panel_top .content = yield
some another partial which will be displayed using prev template (all this stuff is rendered using AJAX, but this doesn't matter)
- content_for :panel_top do .title.left = title content text
and this worked like a charm in Rails 3.0
But, after upgrade to 3.2 this fails! Yiels just yields "content text", so I have "content text" twice and no title at all
only changing = yield :panel_top
to = content_for :panel_top
works for 3.2
I am not sure that this solution is ok, and if it is stable or recommended, I cannot find any notes about changes in yield
processing nor in Rails 3.1 release notes, nor in 3.2 ones.
Can you help what is the best way to organize yield
ing inside partials?
回答1:
From Rails 3.0 to Rails 3.2 content_for
was really changed:
3.0:
def content_for(name, content = nil, &block)
content = capture(&block) if block_given?
@_content_for[name] << content if content
@_content_for[name] unless content
end
3.2:
def content_for(name, content = nil, &block)
if content || block_given?
content = capture(&block) if block_given?
@view_flow.append(name, content) if content
nil
else
@view_flow.get(name)
end
end
This shows us, that from 3.2 content_for
works for showing/inserting content too, not only store it for named section.
Also, if you make an attempt to debug yield
logic you'll se that it yields before content_for
is correctly initialized.
So, leaving fragment caching out of this discussion I can conclude that content_for
is preferrable way to insert named sections anywhere except top-level layouts. In helpers and other situations yield
should render wrong results.
来源:https://stackoverflow.com/questions/9185344/content-for-vs-yield-in-partials