ruby-on-rails - 非常长的 html 属性的 Haml 多行

标签 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

显然,这样更好,但我更愿意将所有内容集中在一个 block 中。有没有办法在不将文本移动到另一个文件的情况下执行此操作?

最佳答案

使用 |对我来说没问题:

%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> s 至 \n让新行在浏览器中正常工作。

关于ruby-on-rails - 非常长的 html 属性的 Haml 多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34687477/

相关文章:

mysql - Rails - JOIN 子句不包含具有空外键的行

ruby-on-rails - 如何将变量传递给哈希字段上的 mongoid 查询

html - 在 CSS : rails 4 + bootstrap 3 中使用 glyphicon 作为背景图片

ruby-on-rails - 为什么 accepts_nested_attributes_for 对我不起作用? ( rails 3)

javascript - rails 4 : append partial to hidden div upon AJAX call

arrays - ruby - 如何生成给定长度的字母和数字的每种组合的数组?

ruby-on-rails - 如何从 ApplicationController (Rails) 访问 cookie

ruby - 如何使用后续声明 postgresql json/jsonb 字段?

ruby-on-rails - 如何将 erb 中的 Rails 整个应用程序转换为 haml

ruby-on-rails - 我怎样才能在一个 block 中产生多个部分?