【问题标题】:Convert Slim code to ERB将 Slim 代码转换为 ERB
【发布时间】:2017-10-26 17:13:21
【问题描述】:

我有这个纤细的代码:

.search-wrapper
  - if content_for?(:fulltext_search)
    = yield(:fulltext_search)
  - else
    - placeholder = nil unless defined? placeholder
    = form_tag collection_path, method: :get
      .input-group style='margin-bottom: 0;'
        = text_field_tag :term, params[:term], placeholder: placeholder || t(:search, default: 'search'), class: 'form-control'
        .btn-group
          button.btn.btn-search tabindex="-1" type='submit' title=t(:search, default: 'search')
            i.fa.fa-search

我想转换为 erb 代码。我已经尝试了一些第一行:

<div class="search-wrapper">
  <% if content_for(:fulltext_search) %>
      <%= yield(:fulltext_search) %>
  <% else %>
  <% placeholder = nil unless defined? placeholder %>
      <%= form_tag collection_path, method: :get %>
  <% end %>
</div>

我正在挣扎的想法是。我不知道如何将此代码转换为erb:

= form_tag collection_path, method: :get
  .input-group style='margin-bottom: 0;'

请告诉我如何转换上面的代码。

谢谢

【问题讨论】:

    标签: ruby-on-rails erb slim-lang


    【解决方案1】:

    在 SLIM 中,每当您看到 .class-name 时,您都可以将其转换为 &lt;div class="class-name"&gt;&lt;/div&gt;

    另外,.input-group 的缩进表示父级form-tag 中的一个块(doend

    在你的情况下,

    = form_tag collection_path, method: :get
      .input-group style='margin-bottom: 0;'
    

    转换为:

      <%= form_tag collection_path, method: :get do %>
        <div class="input-group" style="margin-bottom:0">
        </div>
      <% end %>
    

    .input-group 下的所有缩进内容都将进入&lt;div&gt; 标签内。

    这是使用erb2slim 工具验证的。

    【讨论】:

      【解决方案2】:

      最简单的方法是使用内置的 erb 转换器。为什么不使用它?

      只要运行:

      slimrb -e your_file.slim
      

      您将所有内容都转换为 erb。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-10
        • 1970-01-01
        • 2023-03-09
        相关资源
        最近更新 更多