简体   繁体   English

使用ERB变量来干燥html

[英]Using ERB variables to DRY up html

I want to DRY up my ERB with the below: 我想用以下方法干燥我的ERB:

<div>
  <section class="item">
    +
    <%= render partial: "layouts/list-item" %>
  </section>
  <section class="item">
    +
    <%= render partial: "layouts/list-item" %>
  </section>
  <section class="item">
    +
    <%= render partial: "layouts/list-item" %>
  </section>
</div>

I want to do something like this to not have to repeat myself but it's throwing an error. 我想做这样的事情,不必重复我自己,但是会引发错误。 Do I have to use a for loop instead? 我是否必须使用for循环?

</div>
  <% item = '      <section class="item">
          +
          <%= render partial: "layouts/list-item" %>
        </section>'
  %>
  <%= item * 3 %>
</div>

You could use : 您可以使用:

<div>
  < ["", "+", "+"].each do |item| >
    <section class="item">
      <%= item >
      <%= render partial: "layouts/list-item" %>
    </section>
  < end >
</div>

For anything more complex, you could define instance variables in controller, and use basic logic in views (as the loop in this example) to display the data from those variables. 对于更复杂的事情,您可以在controller中定义实例变量,并在视图中使用基本逻辑(在本示例中为循环)以显示这些变量的数据。

Data should come from a model via controller, html elements should be in views or helpers. 数据应通过控制器来自模型,html元素应位于视图或帮助器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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