Echoing HTML code whilst writing a ruby function

Not sure the title is clear enough, but let’s say I have the following block

<% if preface.nil? || preface.empty? %>
    <p><%= preface %></p>
<%end%>

Instead of keep opening and closing the ruby tags, I’d like to echoing the HTML code thus making the code more readable.

So I’d like something like

<% if preface.nil? || preface.empty?
    print preface
end%>

However, both print and put return the result in the console, whereas the concat that I read to be useful it doesn’t work. Any help, please?

concat should work—

<!-- source/index.html.erb -->

<%
  concat 'foo'
  concat 'bar'
%>
<!-- build/index.html -->

foobar

—but ERB is typically written in the style of your first example. If you’d like you can abstract out that pattern into a helper:

# helpers/content_helpers.rb

require 'active_support/core_ext/object/blank'

module ContentHelpers
  def present_content_tag(name, content = nil, options = nil)
    content_tag(name, content, options) if content.present?
  end
end
<!-- source/index.html.erb -->

<%= present_content_tag(:p, 'foo') %>
<%= present_content_tag(:p, '   ') %>
<!-- build/index.html -->

<p>foo</p>

See also content_tag and present?.

I will try again with the concat. First time I did, i got some compilation error. Perhaps because I was trying to “concatenate” multiple strings?