Rails:是否可以使用HAML语法编写视图助手? [英] Rails: Is it possible to write view helpers with HAML syntax?
问题描述
在重构期间,仅复制一部分HAML模板并将其粘贴到帮助者的代码中将非常方便.当前在这种情况下1)我必须从头开始重写那部分视图2)我必须使用冗长的语法,例如content_tag或haml_tag.
During refactoring it would be quite handy just to copy part of HAML template and paste it to helper's code. Currently in such cases 1) I have to rewrite that part of view from scratch 2) I have to use that verbose syntax like content_tag or haml_tag.
我知道有可能使用HAML systax定义充当帮助程序的局部.尽管1)对我来说,为每个小小的微型函数创建一个单独的文件是不方便的,但2)局部的调用语法非常冗长.
I know that it's possible to define partials with HAML systax that will serve as helper. Though 1) as for me it's inconvinient to create a separate file for each small tiny function 2) invocation syntax for partial is quite verbose.
理想情况下,我希望我的* _helper类看起来像这样:
Ideally i'd like my *_helper class to look like this:
- 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
}
有没有可以解决我问题的插件?
Is there a plugin that solves my issue?
或者,也许您可以描述如何将HAML代码段重构为可重用的元素(帮助程序/函数/部分/构建器/等)?
Alternatively, maybe you can describe how do you refactor HAML snippets to reusable elements (helpers/functions/partials/builders/etc)?
推荐答案
来自参考:
def render_haml(code)
engine = Haml::Engine.new(code)
engine.render
end
这将启动一个新的Haml引擎并进行渲染.
This initiates a new Haml engine and renders it.
这篇关于Rails:是否可以使用HAML语法编写视图助手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!