【发布时间】:2014-11-27 21:53:18
【问题描述】:
我正在构建一个带有部分的面板,但我想用一些 html 自定义面板的内部。有没有办法为哈希值编写一些 HTML?
我有一个自定义的部分 _panel_builder.html.erb,我将其作为参数 pclass、heading、body 等,我想像这样使用它:
(下面的语法不好,但我真的不明白我怎么能做得很好..)
<% @etudes.each_with_index do |etude, i| %>
<%= render 'shared/panel_builder',
pclass: panel_color_rotation(i),
heading: etude.name,
# For the body param, I'd like to be able to use some HTML with occasional <%=...%> for variables, like :
body: (%>
<p><%=etude.description %></p>
<ul>
<%etude.competences.each do |comp| %>
<li><strong><%= competence.name %></strong> : <%=competence.level %>
<br><small><%=competence.why %></small>
</li>
<% end %>
</ul>
<%).html_safe,
collapsable: true %>
<% end %>
编辑:我的 _panel_builder 部分外观的想法:
<%
collapsable ||= false
pclass ||= "default"
footer ||= false
%>
<div class="panel panel-<%= pclass %> <%= if collapsable then "panel-collapsable " end %>">
<div class="panel-heading">
<%= heading %>
<% if collapsable %>
<span class="pull-right"><i class="glyphicon glyphicon-chevron-down"></i></span>
<% end %>
</div>
<div class="panel-body <%= if collapsable then "collapse" end %>">
<%= body %>
</div>
<% if footer %>
<div class="panel-footer <%= if collapsable then "collapse" end %>">
<%= footer %>
</div>
<% end %>
</div>
【问题讨论】:
-
为什么这不是一个带块的助手?指定这样的身体有各种错误。
-
问题是,在我的例子中,我使用了一个使用 3 个非常抽象的局部变量(
heading、body、footer)的局部变量,我希望能够有一些其中 3 个的 HTML(未编写为 Ruby 字符串)... -
@Dave 您指的是
capture助手吗?我刚刚在 ruby doc 网站上发现了这个,似乎是我想要的 :)
标签: html ruby-on-rails ruby erb