简体   繁体   English

将红宝石块传递给HAML中的部分对象

[英]Pass ruby block to partial in HAML

I have made a partial for generating some of the layout of my page, and I have in the partial a space where I would need to insert a description. 我已经制作了一个局部页面,用于生成页面的某些布局,并且在局部页面中有一个空间需要插入描述。

The description is static text from the caller of the partial, and it's pretty long. 描述是来自部分调用者的静态文本,并且相当长。 so I would like to write it as a block of text. 所以我想把它写成一段文字。

This is what I have: 这就是我所拥有的:

= 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

but when I try rendering the page I get: 但是当我尝试渲染页面时,我得到:

can't find string "BLOCK" anywhere before EOF

I would rather use a yield and provide the content in a content_for block. 我宁愿使用yield并在content_for块中提供内容。

In your partial: 在您的部分中:

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

Then in your view: 然后在您看来:

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

This Rails code passes a "block"/heredoc to a partial within a HAML view: 该Rails代码将“ block” / 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

(using HAML 4.0.5 and Rails 3.2.13) (使用HAML 4.0.5和Rails 3.2.13)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM