【问题标题】:Some HTML for values of Hash keys哈希键值的一些 HTML
【发布时间】:2014-11-27 21:53:18
【问题描述】:

我正在构建一个带有部分的面板,但我想用一些 html 自定义面板的内部。有没有办法为哈希值编写一些 HTML?

我有一个自定义的部分 _panel_builder.html.erb,我将其作为参数 pclassheadingbody 等,我想像这样使用它:

(下面的语法不好,但我真的不明白我怎么能做得很好..)

<% @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 个非常抽象的局部变量(headingbodyfooter)的局部变量,我希望能够有一些其中 3 个的 HTML(未编写为 Ruby 字符串)...
  • @Dave 您指的是capture 助手吗?我刚刚在 ruby​​ doc 网站上发现了这个,似乎是我想要的 :)

标签: html ruby-on-rails ruby erb


【解决方案1】:

好的,我实际上是在寻找 capture 助手:

<% @etudes.each_with_index do |etude, i| %>
<%= render 'shared/panel_builder', 
    pclass: panel_color_rotation(i),
    heading: etude.name,
    body: capture do %>
        <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>
    <% end %>,
    collapsible: true %>
<% end %>

注意:在某些情况下,我还想传递一个复杂的 HTML 块作为页脚的页眉,所以我不能只使用一个带块的助手。

注意 2:我的面板是为通用数据制作的 HTML 模板。我无法将 Ruby 对象传递给它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 2013-07-23
    • 2015-11-12
    • 2013-03-22
    • 2011-02-15
    • 2014-05-02
    相关资源
    最近更新 更多