Haml:控制文本周围的空白

Mat*_*chu 96 haml ruby-on-rails

在我的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
Run Code Online (Sandbox Code Playgroud)

接近的模板:

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

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

Boo*_*age 208

通过Haml的帮手介绍了更好的方法:

环绕

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

成功:

click
= succeed '.' do
  %a{:href=>"thing"} here
Run Code Online (Sandbox Code Playgroud) 生产:
click
<a href='thing'>here</a>.
Run Code Online (Sandbox Code Playgroud)

先于:

= precede '*' do
  %span.small Not really
Run Code Online (Sandbox Code Playgroud) 生产:
*<span class='small'>Not really</span>
Run Code Online (Sandbox Code Playgroud)

回答原来的问题:

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
Run Code Online (Sandbox Code Playgroud) 生产:
I will first
<a href="http://example.com">link somewhere</a>,
then render this half of the sentence if a condition is met
Run Code Online (Sandbox Code Playgroud)

  • 我觉得我错过了一些东西(在查看upvotes计数时),但*succeed*变体不等同于原始变体,因为即使`@condition == false`也会渲染尾随逗号,这更难看比这个逗号前的空格. (16认同)
  • 我设法通过使用先行而不是成功来获得正确的结果。干杯! (2认同)

Boo*_*age 39

您也可以使用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
Run Code Online (Sandbox Code Playgroud)

生产:

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

但是,正如您所看到的,>修饰符还会删除链接前面的空白,从而删除单词和链接之间所需的空格.我还没有找到一个漂亮的方法,除了添加&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
Run Code Online (Sandbox Code Playgroud)

最终产生所需的输出而没有大量难以读取的插值:

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
Run Code Online (Sandbox Code Playgroud)

  • 它适用于标签但不适用于表达式; 在您的示例中,您已将其表达式更改为标记.我有同样的问题,不幸的是,这不是一个解决方案. (3认同)

Mat*_*chu 12

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

帮手

def one_line(&block)
  haml_concat capture_haml(&block).gsub("\n", '').gsub('\\n', "\n")
end
Run Code Online (Sandbox Code Playgroud)

视图

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
Run Code Online (Sandbox Code Playgroud)

这样,默认情况下会排除空格,但我仍然可以使用"\n"行明确地包含它.(它需要双反斜杠,因为否则HAML会将其解释为实际的换行符.)让我知道是否有更好的选择!


Yo *_*dke 6

您可以使用HAML的'aligator语法'

空白删除:>和<

并且<让您更好地控制标签附近的空白.>将删除标记周围的所有空格,而<将删除标记内的所有空格.您可以将它们视为鳄鱼吃空白:>面对标签并吃掉外面的空白,然后<面对标签并吃掉内部的空白.它们放在标签定义的末尾,在类,id和属性声明之后,但在/或=之前.

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


Chu*_*uck 5

一旦接近我采取的这种事情是使用字符串插值:

I will first #{link_to 'Link somewhere'}#{', then render this half of the sentence if a condition is met' if condition}
Run Code Online (Sandbox Code Playgroud)

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


the*_*man 5

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

%a{:href => 'http://example.com'}>= ' link somewhere'
Run Code Online (Sandbox Code Playgroud)

这个空间在引号中.