在HAML中将红宝石块传递给部分

时间:2014-01-28 19:59:06

标签: ruby-on-rails ruby block haml partial-views

我已经部分地生成了我的页面的一些布局,并且我在部分空间中需要插入描述。

描述是来自部分调用者的静态文本,而且很长。所以我想把它写成一个文本块。

这就是我所拥有的:

= render "feature", title: "Planning", text: <<-BLOCK
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
BLOCK

但是当我尝试渲染页面时,我得到了:

can't find string "BLOCK" anywhere before EOF

2 个答案:

答案 0 :(得分:1)

我宁愿使用yield并在content_for块中提供内容。

在你的部分:

%p Other content wrapping the block of text
= yield :text_block
%p Other content wrapping the block of text

然后在你看来:

= content_for :text_block do
  The content you need to insert goes here ...

答案 1 :(得分:1)

此Rails代码将“块”/ heredoc传递给HAML视图中的部分:

- Haml::Template.options[:suppress_eval] = false
:ruby
  haml_io.puts render "feature", title: "Planning", text: <<BLOCK
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  BLOCK

(使用HAML 4.0.5和Rails 3.2.13)