【问题标题】:Haml: Control whitespace around textHaml:控制文本周围的空格
【发布时间】:2010-11-21 15:18:11
【问题描述】:

在我的 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

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

【问题讨论】:

    标签: ruby-on-rails haml


    【解决方案1】:

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

    【讨论】:

    • 帮助者究竟如何工作?嗯,我会看看我能想出什么......
    • 至于空格咀嚼,如果它不在某种标签定义上,我无法弄清楚如何使该语法工作。是我做错了,还是没有标签时该语法不起作用?
    【解决方案2】:

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

    助手

    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 会将其解释为实际的换行符。)如果有更好的选择,请告诉我!

    【讨论】:

    • 请注意:我最终明智地将这些转移给了助手:P
    • 对世界的另一个说明:这些天,我使用 Groxx 的解决方案 :)
    • 这对于生成文本文件的haml非常有用!在我的情况下,我有一条线,其中一部分是由“if”决定的,我无法用 mysamillidea 的解决方案修复它,因为它没有删除换行符,它只是将逗号移动到换行符之前。 (虽然我同意对于原始问题的答案,mysmallidea 是最好的。)
    【解决方案3】:

    曾经我对这类事情采取的方法是使用字符串插值:

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

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

    【讨论】:

    • 嗯。这是我对此类事情的一般处理方法,但我从未想过在其中使用条件。很遗憾,我使用的实际模板比句子的后半部分要复杂一些......但这绝对值得记住 - 谢谢!
    【解决方案4】:

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

    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
    

    但是,如您所见,&gt; 修饰符还会去除链接前面的空格,从而删除单词和链接之间所需的空格。我还没有想出一个很好的解决方法,除了在“I will first”的末尾添加&amp;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
    

    【讨论】:

    • 忘了说我是从 Haml 备忘单中得到的,这很有帮助:cheat.errtheblog.com/s/haml
    • 它适用于标签但不适用于表达式;在您的示例中,您已将他的表达式更改为标签。我有同样的问题,不幸的是这不是一个解决方案。
    • 我会说&amp;nbsp; 有一个特殊的含义,它不是一个普通的空格 - 它是不间断的空格,这意味着在自动换行期间浏览器会尽一切努力保持单词与&amp;nbsp; 在一起,这并不总是你想要的。
    • 除了 Andrew 的评论,使用 &amp;#032; 而不是 &amp;nbsp; 只是一个纯空白。
    【解决方案5】:

    您可以这样做以保留前导空格:

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

    空格在引号中。

    【讨论】:

      【解决方案6】:

      虽然没有很好的文档记录,但这是使用 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 字符(但它仍然比使用帮助程序更有效)。

      【讨论】:

        【解决方案7】:

        我遇到了类似的问题并发现了这个问题,所以我想我会发布另一个不需要辅助方法的解决方案。使用 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,它也可能适用于早期版本。

        【讨论】:

        • 诚然,Haml 不是为内容而设计的。不过,这不是我们在那里谈论的内容。这是一个模板。那篇博文的作者指的是用 Haml 编写一个完整的静态网页之类的东西,这不是我正在做的事情。我提供的代码 sn-p 几乎是完整的.haml 文件——事实上它包含一个链接和一个逗号并不能真正表明任何一种方式。
        • 啊,我明白了。我已经编辑了我的答案,让解决方案独立存在。
        • 虽然这可能有效,但我认为这会导致标记难以阅读。相反,只需使用其他人提到的 HAML 空格修饰符 ,这可以使您的 HAML 保持干净和可读。
        【解决方案8】:

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

        surround

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

        succeed:

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

        precede:

        = 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
        

        【讨论】:

        • 好时机,我刚刚通过阅读 Haml 的源代码发现了这些。显然他们已经存在了一段时间。奇怪的是他们没有在主要参考页面中记录它们......
        • 您能否扩展您的答案并展示以使用这些助手(大概是succeed)来表达OP的示例?对我来说,它似乎仍然不明显而且有点难看:gist.github.com/1665374
        • 我觉得我遗漏了一些东西(在查看赞成票时),但 succeed 变体并不等同于原始变体,因为尾随逗号甚至会被渲染if@condition == false,比这个逗号前的空格更丑。
        • 我设法通过使用先行获得正确的结果,而不是成功。干杯!
        【解决方案9】:

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

        - if @condition
          %span> , then some more text after the link.
        

        【讨论】:

          【解决方案10】:

          你也可以这样做:

          = link_to url_path do 
            = ["part_1", "part_2"].join(", ")
          

          【讨论】:

            【解决方案11】:

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

            空格去除:> 和

            和 将删除标签周围的所有空格,而 面向标签外并吃掉外面的空白,

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

            【讨论】:

              【解决方案12】:

              我得到的解决方案是:

              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代码的结果,但在这里它会服务于目的。

              【讨论】:

                【解决方案13】:

                preserve 函数对我有用

                .white-space-pre= preserve "TEXT"

                【讨论】:

                  猜你喜欢
                  • 2017-11-17
                  • 2015-03-05
                  • 2018-08-03
                  • 2014-04-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-04-28
                  • 2012-09-07
                  相关资源
                  最近更新 更多