简体   繁体   中英

How do I format HTML, removing unwanted line breaks?

I would like to do something like this:

<div class="show_details_block">
  <% if @model.details.length > 0 then %>
    <%= h(@model.details) %>
  <% else %>
    "No details available"
  <% end %>
</div>

However, this adds unwanted line breaks to my HTML source. I would prefer to avoid writing it inline like this:

<div class="show_details_block"><% if @task.details.length <= 0 then %>"No details available"<% else %><%= h(@task.details) %><% end %></div>

Can I maintain my code formatting/styling without having to face the consequences of unwanted line breaks?

Haml counterpart:

.show_details_block
  - if @model.details.length > 0 then
    = h(@model.details)
  - else
    No details provided..

ERB allows you to use <%- and -%> around your code interpolation. These forms suppress the line-breaks before or after the start/end interpolation marks. See the description of trim-mode in ERB::new

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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