ERB样式的最佳实践

时间:2012-06-23 04:47:16

标签: ruby-on-rails ruby styles indentation erb

我想获得一些关于样式化ERB模板的最佳实践的社区意见。

  • 我使用Tab宽度为2.
  • 我使用软标签(空格)。
  • 我使用80的Word Wrap。

除了我的ERB模板中的Ruby代码之外,我还缩进了我的所有HTML标记。

这通常会使ERB非常易读。

假设上面或类似的参数,你更喜欢缩进更长的线?

考虑这个例子:

<div class="content">
  <div class="row">
    <div class="span10">
      <%= simple_form_for(@user, html: { class: 'form-horizontal' }) do |f| %>
        <%= f.input :MembershipID, label: 'Membership ID :', hint: 'Use the user\'s official Membership ID if they have one. Otherwise, enter their phone number (e.g. 2125551234)', input_html: { value: @user[:MembershipID] } %>
      <% end %>
    </div>
  </div>
</div>

f.input 一行非常丑陋且不可读。

我认为这样的事情是理想的,但我想在改变我的风格之前得到一些反馈。

<div class="content">
  <div class="row">
    <div class="span10">
      <%= simple_form_for(@user, html: { class: 'form-horizontal' }) do |f| %>
        <%= f.input :MembershipID, 
                label: 'Membership ID :', 
                hint: 'Use the user\'s official Membership ID if they have one. Otherwise, enter their phone number (e.g. 2125551234)', 
                input_html: { value: @user[:MembershipID] } %>
      <% end %>
    </div>
  </div>
</div>

我是否来自ERB的双缩进开始标记&lt;%= 或帮助名称 f.input 是否更好。

请称重! (请不要将其转变为ERB与HAML辩论,仅假设ERB!)

感谢。

2 个答案:

答案 0 :(得分:2)

需要考虑的一些事项:

  • 请勿使用“span10”和“row”类,而应将其应用于CSS
  • 使用帮助程序,即使您不打算重复使用它们(目前),它也会清理您的代码

这给你类似的东西:

<div class="content">
  <%= simple_form_for(@user) do |f| %>
    <%= membership_input_field %>
  <% end %>
</div>

SCSS:

.content {
  @extend .row;
  #users_form {
    @extend .span10;
    @extend .form-horizontal;
  }
}

我现在无法测试,但你应该得到一般的想法。更清晰,更无用的类来设计HTML格式。

答案 1 :(得分:1)

对于长期或复杂的ERB扩展,我经常像这样多线:

<div>
  <div>
    <%=
      some_call_with_lots_of_args_or_a_block(
        arg1,
        arg2
      ) do
        block_stuff()
      end
    %>
  </div>
</div>

很多行,但现在对于给定的“感觉”值,缩进现在都“有意义”。 HTH!