Skip to content

Instantly share code, notes, and snippets.

@renatocassino
Created October 11, 2016 17:17
Show Gist options
  • Save renatocassino/511ca0558ba81540c908e2dced08ca9e to your computer and use it in GitHub Desktop.
Save renatocassino/511ca0558ba81540c908e2dced08ca9e to your computer and use it in GitHub Desktop.
module ApplicationHelper
def spaceless(&block)
contents = capture(&block)
# Note that string returned by +capture+ is implicitly HTML-safe,
# and this mangling does not introduce unsafe changes, so I'm just
# resetting the flag.
if Rails.env.production? then
contents.strip.gsub(/>\s+</, '><').html_safe
else
contents.html_safe
end
end
end
# In view:
<%= spaceless do %>
<!-- ........ -->
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment