我想我为自己创造了一个令人讨厌的问题。见下文:
查看
<% tl = tablist_initialize(params[:tab], 'profile') %>
# lots of html
# ...
<%= tl.tab_content do %>
<% tl.tab_panel 'profile' do %>
Hello!
<% end %>
<% tl.tab_panel 'settings' do %>
Sup!
<% end %>
<% tl.tab_panel 'notifications' do %>
Nothing!
<% end %>
<% end %>
辅助
class TabList
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper
attr_accessor :output_buffer
def initialize(tab, primary)
@current_tab = tab
@primary = primary
end
def tab_content(&block)
content_tag(:div, :class => "tab-content", &block)
end
def tab_panel(id, &block)
concat(content_tag(:div, {:class => (@current_tab == id ? "tab-pane active" : "tab-pane"),
:role => "tabpanel", :id => id }, &block))
end
# other methods
# ...
end
输出
我的计划是轻松创建引导标签。结果如下:
Hello! Sup! Nothing!
<div class="tab-content">
<div class="tab-pane active" role="tabpanel" id="profile">
Hello!
</div>
<div class="tab-pane" role="tabpanel" id="settings">
Sup!
</div>
<div class="tab-pane" role="tabpanel" id="notifications">
Nothing!
</div>
</div>
为什么结果的第一行(Hello!Sup!Nothing!)?我认为输出内部块两次是奇怪的:一次正确,一次完全在任一块之外(在开始时)。我尝试了不同的方法:省略concat函数,在第一个content_tag中使用concat around yield,但所有这些尝试都没有给我希望的结果。
答案 0 :(得分:0)
删除tl.tab_content行上的=并在每个tl.tabl_panel行上添加=。
<% tl.tab_content do %>
<%= tl.tab_panel 'profile' do %>
Hello!
<% end %>
<%= tl.tab_panel 'settings' do %>
Sup!
<% end %>
<%= tl.tab_panel 'notifications' do %>
Nothing!
<% end %>
<% end %>
另外,从您的tab_panel方法中删除concat
。
class TabList
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper
attr_accessor :output_buffer
def initialize(tab, primary)
@current_tab = tab
@primary = primary
end
def tab_content(&block)
content_tag(:div, :class => "tab-content", &block)
end
def tab_panel(id, &block)
content_tag(:div, {:class => (@current_tab == id ? "tab-pane active" : "tab-pane"), :role => "tabpanel", :id => id }, &block)
end
# other methods
# ...
end