在重构过程中,只需复制HAML模板的一部分并将其粘贴到helper的代码中,这将非常方便。目前,在这种情况下,1)我必须从头开始重写视图的那一部分2)我必须使用像content_tag或haml_tag这样的冗长语法。
我知道可以用HAML systax来定义部分,它将作为helper。尽管1)对我来说,为每个小函数创建一个单独的文件是不方便的。2) partial的调用语法相当冗长。
理想情况下,我希望我的*_helper类看起来像这样:
- def some_helper(*its_args)
.some_class
= some_ruby_expression
%some_tag#some_id
- another_expression do
etc
或者至少像这样:
define_haml_helper :some_helper, [:arg1, :arg2], %{
.some_class
= some_ruby_expression
%some_tag#some_id
- another_expression do
etc
}
有没有插件可以解决我的问题?
或者,也许您可以描述如何将HAML片段重构为可重用的元素(helpers/function/partials/builders/等)?
发布于 2011-03-09 20:41:09
def render_haml(code)
engine = Haml::Engine.new(code)
engine.render
end
这将启动一个新的Haml引擎并渲染它。
发布于 2011-03-31 05:18:30
如果您所追求的只是一种用于小的可重用片段的方法,那么带有局部变量的偏量又如何呢?http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
发布于 2013-10-11 03:45:32
Haml现在有了一个capture_haml
方法,您可以使用它来完成此任务。
def some_helper
capture_haml do
.some_class
= yield
#some-code-after
end
end
some_helper do
%h1 Hello World
end
=> <div class="some_class">
<h1>Hello World</h1>
</div>
<div id="some-code-after"></div>
以下是有关capture_haml
的更多信息的链接:http://haml.info/docs/yardoc/Haml/Helpers.html#capture_haml-instance_method
https://stackoverflow.com/questions/5245432
复制相似问题