如何用`修剪前导空格

问题描述 投票:0回答:1

(据我在这里研究过,这不是一个重复的问题。修剪空间 - 往往是尾随换行 - 正在讨论<%--%>,但不是<%=。它也可能是Erubi模板引擎中的一个小缺陷,Rails用于ERB模板的那个。)

我想在视图中渲染/语法高亮显示代码,我的ERB视图模板包含:

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

结果是HTML输出是:

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

由于pre标记,第一个代码行前面的空格包含在HTML中并因此呈现,导致第一个代码行缩进四个空格太多。

显然,我也可以将ERB视图模板设为:

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

但是在我的模板视图中看起来很难看(因为缩进是关闭的)。

问题:我如何让<%=吞下领先的空间?我知道使用-%>作为结束标记会删除尾随空格/换行符...但我希望删除前导空格(不仅仅是换行符)。

ruby-on-rails erb actionview erubis
1个回答
1
投票

尝试使用带有ERZ标记的concat helper method,该标记以<%而不是<%=开头:

<p>
  <strong>Code:</strong>
  <pre class="highlight github">
    <% concat(highlight(@code.code, @code.language)) %>
  </pre>
</p>
© www.soinside.com 2019 - 2024. All rights reserved.