98

在我的 Rails 模板中,我想使用 HAML 完成最终的 HTML:

I will first <a href="http://example.com">link somewhere</a>, then render this half of the sentence if a condition is met

接近的模板:

I will first
= link_to 'link somewhere', 'http://example.com'
- if @condition
  , then render this half of the sentence if a condition is met

但是,您可能会注意到,这会在链接和逗号之间产生一个空格。有什么实用的方法可以避免这个空格吗?我知道有删除标签周围空格的语法,但是同样的语法可以应用于文本吗?我真的不喜欢额外标记的解决方案来实现这一点。

4

13 回答 13

212

通过 Haml 的助手介绍了一种更好的方法:

环绕

= surround '(', ')' do
  %a{:href => "food"} chicken
产生:
(<a href='food'>chicken</a>)

成功

click
= succeed '.' do
  %a{:href=>"thing"} here
产生:
click
<a href='thing'>here</a>.

先于

= precede '*' do
  %span.small Not really
产生:
*<span class='small'>Not really</span>

要回答原始问题:

I will first
= succeed ',' do
  = link_to 'link somewhere', 'http://example.com'
- if @condition
  then render this half of the sentence if a condition is met
产生:
I will first
<a href="http://example.com">link somewhere</a>,
then render this half of the sentence if a condition is met
于 2011-06-08T18:56:15.117 回答
40

你也可以使用 Haml 的“trim whitespace”修饰符来做到这一点。在 Haml 声明之后插入>将防止在其周围添加空格:

I will first
%a{:href => 'http://example.com'}> link somewhere
- if @condition
  , then render this half of the sentence if a condition is met

产生:

I will first<a href='http://example.com'>link somewhere</a>, then render this half of the sentence if a condition is met

但是,如您所见,>修饰符还去除了链接前面的空格,删除了单词和链接之间的所需空格。我还没有想出一个很好的方法来解决这个问题,除了添加&nbsp;到“我会先”的末尾,就像这样:

I will first&nbsp;
%a{:href => 'http://example.com'}> link somewhere
- if @condition
  , then render this half of the sentence if a condition is met

最终产生所需的输出,而无需大量难以阅读的插值:

I will first&nbsp;<span><a href="http://example.com">link somewhere</a></span>, then render this half of the sentence if a condition is met
于 2009-10-07T11:30:39.463 回答
12

好吧,这是我正在解决的解决方案:

帮手

def one_line(&block)
  haml_concat capture_haml(&block).gsub("\n", '').gsub('\\n', "\n")
end

看法

I will first
- one_line do
  = link_to 'link somewhere', 'http://example.com'
  - if @condition
    , then render this half of the sentence
    \\n
    if a condition is met

这样,默认情况下会排除空格,但我仍然可以使用“\n”行显式包含它。(它需要双反斜杠,否则 HAML 会将其解释为实际的换行符。)如果有更好的选择,请告诉我!

于 2009-08-21T12:16:46.450 回答
6

您可以使用 HAML 的“aligator 语法”

空格删除:> 和 <

和 < 让您可以更好地控制标签附近的空白。> 将删除标签周围的所有空格,而 < 将立即删除标签内的所有空格。你可以把它们想象成鳄鱼吃空白: > 面向标签外并吃掉外面的空白,< 面对标签并吃掉里面的空白。它们被放置在标签定义的末尾,在类、id 和属性声明之后,但在 / 或 = 之前。

http://haml.info/docs/yardoc/file.REFERENCE.html#whitespace_removal__and_

于 2015-02-24T12:40:50.283 回答
5

一旦我对这种事情采取的方法是使用字符串插值:

I will first #{link_to 'Link somewhere'}#{', then render this half of the sentence if a condition is met' if condition}

我不喜欢插值中文字字符串的外观,但我之前已经将它与先前声明的字符串或动态生成的字符串一起使用。

于 2009-08-21T16:49:41.523 回答
5

您可以这样做以保持领先空间:

%a{:href => 'http://example.com'}>= ' link somewhere'

空格在引号中。

于 2009-10-17T03:18:26.743 回答
3

虽然没有很好的文档记录,但这是使用 HAML 空白保留 (>) 结合 ASCII 空间 (& #32;) 实现的,而不是使用助手:

%a{:href=>'/home'}> Home link
,&#32; 
%a{:href=>'/page'} Next link

这将产生你想要的:

<a href='/home'>Anchor text</a>,&#32;
<a href='/page'>More text</a>

但我同意,HAML 需要提出一种更好的方法来做到这一点,因为它确实向页面添加了不必要的 ASCII 字符(但它仍然比使用帮助程序更有效)。

于 2009-11-16T08:24:47.660 回答
1

有尖括号“空格咀嚼”语法,否则为它编写一个辅助方法。

于 2009-08-21T11:49:30.127 回答
1

我过去使用的另一个选项:

- if @condition
  %span> , then some more text after the link.
于 2013-04-13T07:37:50.017 回答
1

我遇到了类似的问题并发现了这一点,所以我想我会发布另一个不需要辅助方法的解决方案。使用 Ruby 插值 #{} 来包装链接和 if 语句:

I will first 
#{link_to 'link somewhere', 'http://example.com'}#{if true : ", then render this half of the sentence if a condition is met" end}

这适用于 3.0.18,它也可能适用于早期版本。

于 2010-09-18T21:36:23.817 回答
0

你也可以总是这样做:

= link_to url_path do 
  = ["part_1", "part_2"].join(", ")
于 2014-09-10T04:13:18.640 回答
0

保留功能对我有用

.white-space-pre= preserve "TEXT"

于 2017-04-20T02:04:13.073 回答
0

我得到的解决方案是:

I will first
= link_to 'link somewhere', 'http://example.com'
- if @condition
  = ", then render this half of the sentence if a condition is met"

您可以使用=, 虽然=用于输出 Rails 代码的结果,但在这里它会达到目的。

于 2015-11-24T10:08:21.350 回答