4

(As far as I have researched here, this is not a duplicate question. Trimming spaces -- often trailing newlines -- is being discussed for <%- or -%>, but not for <%=. It could be a minor defect in Erubi template engine as well, the one being used by Rails for ERB templates.)

I want to render / syntax-highlight code in a view, and my ERB view template contains:

<p>
  <strong>Code:</strong>
  <pre class="highlight github">
    <%= highlight(@code.code, @code.language) %>
  </pre>
</p>

The result is that the HTML output is:

<p>
  <strong>Code:</strong>
  <pre class="highlight github">
    <span class="kt">[and here's the code, but indented too much]</span>
  </pre>
</p>

Because of the pre tag, the spaces in front of the first code line are included in the HTML and thus rendered, resulting that the first code line is indented with four spaces too much.

Obviously, I can also make the ERB view template as:

<p>
  <strong>Code:</strong>
  <pre class="highlight github">
<%= highlight(@code.code, @code.language) %>
  </pre>
</p>

but that looks ugly (because the indenting is off) in my template view.

Question: how can I make the <%= also swallow leading spaces? I know that using -%> as closing tag removes trailing spaces/newlines... but I want the leading spaces (not just newlines) to be removed as well.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Jochem Schulenklopper
  • 6,452
  • 4
  • 44
  • 62
  • 1
    Unlike the rest of html, whitespace in `
    ` blocks matters (that's what the tag _means_, "preformatted content here, I want it __exactly__ like this"). So you may have to learn to live with ugly indentation in your views. Hide it in a partial if you want, and add a comment which explains the ugliness.
    – Sergio Tulentsev Nov 06 '18 at 08:47
  • Thanks @SergioTulentsev! Yes, I know that `pre` should display its contents as pre-formatted, and thus exactly as included. The question is whether I could change a bit of that via the `<%=` tag (or with some variation of that). For example, a `-%>` closing tag eats trailing whitespace (irrespective of being included in the `pre` element), and a `<%-` opening tag eats leading newlines, so I wanted something to eat leading spaces as well. That, or accepting ugliness, whichever is easier :-) – Jochem Schulenklopper Nov 06 '18 at 09:31
  • 1
    I googled for a bit and found all the same posts. There does not appear to be a way to do this with a `<%=` :/ – Sergio Tulentsev Nov 06 '18 at 09:41
  • 1
    This should be possible with a custom rendering helper. – Sergio Tulentsev Nov 06 '18 at 09:48
  • Alas. Thanks for confirming that. If you could votes this question up, we prevent it from being closed (as duplicate), and because of that continue to receive attention from others. – Jochem Schulenklopper Nov 06 '18 at 09:48
  • Question: if this is a valid use case, shall I log an issue (feature request or defect) against Erubis / Erubi? – Jochem Schulenklopper Nov 06 '18 at 09:49
  • 1
    It is valid, but very rare. I only had to do this once in my career. I'd submit a ticket, yes. At worst, you'll waste 10 minutes of your life and nothing comes out of it. – Sergio Tulentsev Nov 06 '18 at 09:52
  • Registered, at https://github.com/jeremyevans/erubi/issues/19. Let's see. If it gets closed, it's fine as well. – Jochem Schulenklopper Nov 06 '18 at 09:58
  • FWIW, tried to replicate this with haml and couldn't make it _keep_ the whitespace. Sounds like a plan B. – Sergio Tulentsev Nov 06 '18 at 09:59
  • Exactly what I was thinking about: Haml seems to handle the similar case better. (I used to use Haml in the past, but for a new project decided to keep the ERB templates, because that's the default Rails sets. Haml gets nicer view templates for sure... – Jochem Schulenklopper Nov 06 '18 at 10:03

1 Answers1

2

Try using the concat helper method with an ERB tag that starts with <% instead of <%=:

<p>
  <strong>Code:</strong>
  <pre class="highlight github">
    <% concat(highlight(@code.code, @code.language)) %>
  </pre>
</p>
tfischbach
  • 3,003
  • 3
  • 22
  • 16