【问题标题】:How to remove whitespace from this code in haml?如何在haml中从此代码中删除空格?
【发布时间】:2012-08-05 10:57:04
【问题描述】:

这是代码

  .meta
    = time_ago_in_words(discussion.created_at)
    ago by
    = discussion.user.username
    |
    = link_to "Edit", edit_discussion_path(discussion), class: "meta"
    |
    = link_to "Delete", discussion_path(discussion), method: :delete, confirm: "Are you sure?", class: "meta"

这是输出:

    <div class='meta'>
      about 7 hours
      ago by
      foo
      |
      <a href="/discussions/2/edit" class="meta">Edit</a>
      |
      <a href="/discussions/2" class="meta" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete</a>
    </div>

这使得输出在| 的两侧具有不平衡的空间。

是否可以删除这些语句之间的空格,而不在所有内容中添加span,或者只使用erbstring interpolation

【问题讨论】:

    标签: ruby-on-rails-3 haml


    【解决方案1】:

    将其显式打印为 Ruby 字符串有帮助吗?

    .meta
      = time_ago_in_words(discussion.created_at)
      ago by
      = discussion.user.username
      = "|"
      = link_to "Edit", edit_discussion_path(discussion), class: "meta"
      = "|"
      = link_to "Delete", discussion_path(discussion), method: :delete, confirm: "Are you sure?", class: "meta"
    

    我的意思是,这不是最好的代码,我更愿意像您最初尝试的那样放置字符,但由于 HAML 强制缩进,您可能必须采用某种不同的路径来指定空白/空格字符。我认为这样做可能会为您提供简单的精确方法,只要在任一侧添加特定数量的空格或 &amp;nbsp; 字符(如果您需要多个)。

    编辑:我注意到标题指定“删除”空格,而不是“平衡”它 - 我的错误。因此,您可以尝试succeed,如下所述:Haml: Control whitespace around textSucceed 没有后续,或者后跟空字符串,应该可以实现你想要的,没有任何空格。

    编辑 2: 你说得完全正确,&gt; 修饰符不适用于 ruby​​ 打印行(如 link to )。 Succeed 应该仍然有效,或者您可以创建一个辅助函数,将所有这些行连接成一个字符串。

    【讨论】:

    • 不,在这种语句的末尾添加&gt; 不是有效的haml。对我不起作用。
    • 你说得对,不知道我在想什么,但 &gt; 修饰符在 ruby​​ 打印行上不可用。最好使用succeed 或带有少量字符串操作的辅助函数。
    【解决方案2】:

    在 adobe dreamwever 中打开你的代码,然后按 ctrl + f ..

    写在下面的代码和勾选正则表达式框

    在查找框中:[\r\n]{2,}

    在替换框中:\n

    n 全部替换...

    尽情享受

    【讨论】:

      【解决方案3】:

      截至目前(2012 年 10 月 21 日),没有办法优雅地做到这一点。

      如果您阅读本文时可以,请务必回答问题,我会将其标记为答案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-05
        • 1970-01-01
        • 1970-01-01
        • 2014-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多