Trần Kim Dự
Trần Kim Dự

Reputation: 6112

Convert Slim code to ERB

I have this slim code:

.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

I want to convert to erb code. I have tried for some first lines:

<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>

The think I am struggling is. I don't know how to convert this code to erb:

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

Please tell me how to converting above code.

thanks

Upvotes: 0

Views: 1658

Answers (2)

three
three

Reputation: 8488

The easiest way is to use the built-in erb converter. Why not use that?

Just run:

slimrb -e your_file.slim

You get everything converted to erb.

Upvotes: 1

Tom Aranda
Tom Aranda

Reputation: 6036

In SLIM, whenever you see .class-name you can convert that to <div class="class-name"></div>

Also, the indentation of .input-group, suggests a block (do and end) in the parent form-tag

In your case,

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

Converts to:

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

All of the indented stuff under the .input-group will go inside the <div> tags.

This was validated using the erb2slim tool.

Upvotes: 1

Related Questions