我有以下两个助手方法:
def hello
capture_haml do
haml_tag :div, 'Hello'
end
end
def hello_world
capture_haml do
hello # How can I call it here?
haml_tag :div, 'World'
end
end我想打电话给hello in hello_world。我单独尝试了hello、capture_haml hello和haml_tag hello,同时也尝试了.html_safe,但是没有一个解决方案是可行的。
我怎么能这么做?
我真的宁愿使用capture_haml而不是直接使用haml_tag,因为我认为在视图中有
= hello_world比
- hello_world谢谢
发布于 2014-07-27 23:25:59
您的hello方法,因为它正在使用capture_haml,所以只返回一个字符串。当您在capture_haml块内调用hello_world方法时,它什么也不做--创建并返回字符串,但根本不使用它。因为它不是写到输出中的,所以它没有被capture_haml捕获。
您可以将字符串写入输出,这将强制capture_haml工作,使用haml_concat,它如下所示:
def hello_world
capture_haml do
haml_concat hello
haml_tag :div, 'World'
end
end这是一个精心设计的例子,但我希望它能显示出正在发生的事情。capture_haml接受一个通常直接写入输出的块(通常是Haml源),并将其作为字符串返回。haml_concat接受一个字符串并将其写入输出,因此在某些方面与capture_haml相反。
发布于 2014-07-27 20:54:28
找到它:
def hello
capture_haml do
haml_tag :div, 'Hello'
end
end
def hello_world
capture_haml do
haml_tag :div, hello
haml_tag :div, 'World'
end
endhttps://stackoverflow.com/questions/24985539
复制相似问题