\n ERB 中的新行转义序列

发布于 12-14 17:35 字数 265 浏览 3 评论 0原文

我不确定为什么“\n\n”不在我的代码中添加两个换行符:

<%= (getparagraph(@geography, "geography")+"\n\n") if @geography != "Other" %> 

为了澄清方法 getparagraphs 只是返回一段文本。我正在尝试在 Ruby 代码中添加两个换行符,而无需使用

有什么想法吗?我读过的所有内容都表明它应该有效。

I'm not sure why "\n\n" is not adding two line breaks in my code below:

<%= (getparagraph(@geography, "geography")+"\n\n") if @geography != "Other" %> 

To clarify the method getparagraphs simply returns a paragraph of text. I'm trying to add two line breaks within the Ruby code without having to use <br/>

Any ideas? Everything I've read implies that it should work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏有森光若流苏2024-12-21 17:35:43

这是在 HTML 中吗?如果是这样,那么只需使用
。 HTML 会忽略多个空白字符,因此您可以有 1 个空格或 50 个换行符,结果都是相同的。

另请注意:除非您使用 XHTML(本身不是一个好主意),否则不要使用自闭合标记,例如
。斜杠无效,因此省略它。

Is this within HTML? If so, then just use <br>. HTML ignores multiple whitespace characters, so you could have one space or 50 newlines and the result would be the same.

Another note: unless you're using XHTML (itself not a great idea), do not use self-closing tags such as <br/>. The slash is invalid, so omit it.

狠疯拽2024-12-21 17:35:42

您的帮助程序的名称中包含“paragraph”,因此也许您应该将其包装在一个段落中,并使用 CSS 在段落周围添加适当的间距:

<% if @geography != "Other" %>
    <p>
      <%= getparagraph(@geography, "geography") %>
    </p>
<% end %>

您始终可以向

添加一个特殊的类如果您在此之后需要额外的间距:

<% if @geography != "Other" %>
    <p class="geo-chunk">
      <%= getparagraph(@geography, "geography") %>
    </p>
<% end %>

然后在您的 CSS 中:

.geo-chunk {
    margin-bottom: 2em; /* Or whatever works */
}

如果这是出现在另一个

中,那么您需要尽可能地重新排列 HTML不要将块元素放入 a 中

<div>
    <!-- The old <p> content ... -->
    <% if @geography != "Other" %>
        <div class="geo-chunk">
          <%= getparagraph(@geography, "geography") %>
        </div>
    <% end %>
</div>

Your helper has "paragraph" in its name so maybe you should wrap it in a paragraph and use CSS to add the appropriate spacing around the paragraph:

<% if @geography != "Other" %>
    <p>
      <%= getparagraph(@geography, "geography") %>
    </p>
<% end %>

You could always add a special class to the <p> if you need an extra spacing after this:

<% if @geography != "Other" %>
    <p class="geo-chunk">
      <%= getparagraph(@geography, "geography") %>
    </p>
<% end %>

and then in your CSS:

.geo-chunk {
    margin-bottom: 2em; /* Or whatever works */
}

And if this is to appear inside another <p> then you'd need to re-arrange the HTML a bit as you can't put a block element inside a <p>:

<div>
    <!-- The old <p> content ... -->
    <% if @geography != "Other" %>
        <div class="geo-chunk">
          <%= getparagraph(@geography, "geography") %>
        </div>
    <% end %>
</div>
始终不够爱げ你2024-12-21 17:35:40

您正在输出 HTML;渲染时空白很大程度上被忽略。

请改用
标签和 .html_safe

You're outputting HTML; whitespace is largely ignored upon rendering.

Use <br/> tags instead, and .html_safe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文