简体   繁体   English

haml_tag直接输出到Haml模板

[英]haml_tag outputs directly to the Haml template

What am I doing wrong with this helper for my HAML template? 我的HAML模板的帮助器我做错了什么?

  def display_event(event)
    event = MultiJson.decode(event)
    markup_class = get_markup_class(event)
    haml_tag :li, :class => markup_class do
      haml_tag :b, "Foo"
      haml_tag :i, "Bar"
    end
  end

This is the error: 这是错误:

haml_tag outputs directly to the Haml template.
Disregard its return value and use the - operator,
or use capture_haml to get the value as a String.

The template is calling display_event like this: 模板调用display_event,如下所示:

 - @events.each do |event|
     = display_event(event)

If I was using regular markup it would expand to the following 如果我使用常规标记,它将扩展到以下内容

%li.fooclass
   %b Foo
   %i Bar

The clue's in the error message: 错误消息中的线索:

Disregard its return value and use the - operator,
or use capture_haml to get the value as a String.

From the docs for haml_tag : 来自haml_tag的文档:

haml_tag outputs directly to the buffer; haml_tag直接输出到缓冲区; its return value should not be used. 不应使用其返回值。 If you need to get the results as a string, use #capture_haml . 如果需要将结果作为字符串获取,请使用#capture_haml

To fix it, either change your Haml to: 要解决此问题,请将Haml更改为:

- @events.each do |event|
  - display_event(event)

(ie use the - operator instead of = ), or change the method to use capture_haml : (即使用-运算符而不是= ),或更改方法以使用capture_haml

def display_event()
  event = MultiJson.decode(event)
  markup_class = get_markup_class(event)
  capture_haml do
    haml_tag :li, :class => markup_class do
      haml_tag :b, "Foo"
      haml_tag :i, "Bar"
    end
  end
end

This will make the method return a string, which you can then display with = in your Haml. 这将使该方法返回一个字符串,然后您可以在Haml中显示=

Note you need to make only one of these changes, if you make both they will cancel each other out and you'll get nothing displayed. 请注意,您需要进行其中一项更改,如果同时进行这两项更改,它们将相互取消,您将无法显示任何内容。

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

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