【问题标题】:haml_tag outputs directly to the Haml templatehaml_tag 直接输出到 Haml 模板
【发布时间】:2012-05-07 20:43:54
【问题描述】:

我的 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

这是错误:

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.

模板调用 display_event 如下:

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

如果我使用常规标记,它将扩展到以下内容

%li.fooclass
   %b Foo
   %i Bar

【问题讨论】:

    标签: ruby-on-rails ruby haml


    【解决方案1】:

    提示在错误信息中:

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

    来自haml_tag 的文档:

    haml_tag 直接输出到缓冲区;不应使用其返回值。如果需要以字符串形式获取结果,请使用#capture_haml

    要修复它,请将您的 Haml 更改为:

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

    (即使用-运算符而不是=),或者将方法更改为使用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
    

    这将使该方法返回一个字符串,然后您可以在 Haml 中使用 = 显示该字符串。

    请注意,您只需进行其中一项更改,如果您同时进行这两项更改,它们将相互抵消,您将不会显示任何内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 2011-02-27
      • 2016-11-01
      • 1970-01-01
      相关资源
      最近更新 更多