Haml multiline用于非常长的html属性

时间:2016-01-08 22:56:48

标签: ruby-on-rails ruby haml

我的编码合作伙伴和我今天花了很多时间试图弄清楚如何重构具有很长属性的链接以使其成为多行:

%a.pull-right{ href: "#", "data-html" => "true", rel: "tooltip", 
  "data-placement" => "left", title: "This is really just so much text. The real thing goes on and on and on, much longer than this. It also contains some line breaks.<br><br>We'd really like to be able to wrap it around so it's readable in the code." }

多行字符串文档中列出的管道方法(+ |)在此上下文中似乎不起作用。我们可以提出的最易读的解决方案是:

:ruby
  tooltip_text = %Q(
    This is really just so much text. The real thing goes on and on 
    and on, much longer than this. It also contains some line breaks.
    <br><br>
    We'd really like to be able to wrap it around so it's readable
    in the code.
  )

= link_to '#', class: 'pull-right', rel: 'tooltip', title: tooltip_text,
  'data-html' => 'true', 'data-placement' => 'left' do

显然,这更好,但我宁愿把它全部放在一个区块里。有没有办法在不将文本移动到另一个文件的情况下执行此操作?

1 个答案:

答案 0 :(得分:2)

使用|对我来说没问题:

%a.pull-right{ href: "#", "data-html" => "true", rel: "tooltip",
  "data-placement" => "left", title: "This is really just so much text. |
  The real thing goes on and on and on, much longer than this. It also  |
  contains some line breaks.<br><br>We'd really like to be able to wrap |
   it around so it's readable in the code." }                           |

输出:

<a class='pull-right' data-html='true' data-placement='left' href='#' rel='tooltip' title="This is really just so much text. The real thing goes on and on and on, much longer than this. It also  contains some line breaks.&lt;br&gt;&lt;br&gt;We'd really like to be able to wrap it around so it's readable in the code."></a>

请注意,即使最后一行也需要在其末尾加上管道符号。

此外,您可能需要将<br>更改为\n以使新行在浏览器中正常运行。