“无空间”相当于 Rails 中的 Django 模板

发布于 2024-11-27 19:28:44 字数 1184 浏览 0 评论 0 原文

请注意以下内容的可读性和平衡:

<li class='aclass anotherclass <%= maybeaconditionalclass %>'>
   <a href="<%= some stuff %>">
     <%= some other stuff %>
   </a>
</li>

不幸的是,它会在链接内产生尾随空格,从而导致难看的尾随下划线。现在,虽然可读性较差,但我可以接受这一点:

<li class='apossibleclass anotherclass <%= maybeaconditionalclass %>'>
   <a href="<%= some stuff %>"><%= some other stuff %></a>
</li>

不过,如果我现在考虑这种事情,同样的问题仍然存在:

li.apossibleclass:after {
    content: "/";
}

因为结束 A 和 LI 之间的空格妨碍了应该粘在列表项末尾的内容。我只能产生丑陋的混乱作为解决方法:

<li class='apossibleclass anotherclass <%= maybeaconditionalclass %>'>
   <a href="<%= some stuff %>"><%= some other stuff %></a></li>

Django想出了一个很好的解决方案: {% spaceless %},所以我正在寻找相当于 {% spaceless %} Rails erb 模板中的标记。

Notice the readability and balance of:

<li class='aclass anotherclass <%= maybeaconditionalclass %>'>
   <a href="<%= some stuff %>">
     <%= some other stuff %>
   </a>
</li>

which unfortunately produces trailing whitespace inside the link resulting in a ugly trailing underline. Now although less readable I can live with this:

<li class='apossibleclass anotherclass <%= maybeaconditionalclass %>'>
   <a href="<%= some stuff %>"><%= some other stuff %></a>
</li>

Still, the same problem remains if I now consider this kind of thing:

li.apossibleclass:after {
    content: "/";
}

as the whitespace between the closing A and LI gets in the way of what should be sticking to my list item's end. I could only produce that ugly mess as a workaround:

<li class='apossibleclass anotherclass <%= maybeaconditionalclass %>'>
   <a href="<%= some stuff %>"><%= some other stuff %></a></li>

Django came up with a nice solution: {% spaceless %}, so I'm looking for the equivalent of the {% spaceless %} tag in Rails erb templates.

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

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

发布评论

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

评论(1

秋意浓 2024-12-04 19:28:44

是的,这将是一个有用的功能,据我所知,Rails 中没有类似的功能。所以我已经编码了。

# Strip all whitespace between the HTML tags in the passed block, and
# on its start and end.
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.
  contents.strip.gsub(/>\s+</, '><').html_safe
end

这是一个帮助程序,您可以将其放置在 application_helper.rb 中,然后像这样使用:

<%= spaceless do %>
  <p>
      <a href="foo/"> Foo </a>
  </p>
<% end %>

... 这将导致像这样的输出字符串

<p><a href="foo/"> Foo </a></p>

遗憾的是,这仅适用于 Rails 3。Rails 2 支持像这样的功能需要对 ERb 进行一些肮脏的修改。

Yes, that would be an useful feature, and as far as I know, there's nothing like it in Rails. So I've coded it.

# Strip all whitespace between the HTML tags in the passed block, and
# on its start and end.
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.
  contents.strip.gsub(/>\s+</, '><').html_safe
end

This is a helper you can place in your application_helper.rb, and then use like this:

<%= spaceless do %>
  <p>
      <a href="foo/"> Foo </a>
  </p>
<% end %>

... which will result in the output string like

<p><a href="foo/"> Foo </a></p>

Sadly, this only works in Rails 3. Rails 2 support for a feature like this will require some dirty hacks to ERb.

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