Rails:是否可以使用 HAML 语法编写视图助手?

发布于 2024-10-21 04:13:26 字数 673 浏览 1 评论 0原文

在重构过程中,只需复制 HAML 模板的一部分并将其粘贴到助手的代码中就会非常方便。目前在这种情况下 1) 我必须从头开始重写视图的该部分 2) 我必须使用像 content_tag 或 haml_tag 这样的详细语法。

我知道可以使用 HAML 系统定义部分作为助手。虽然 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 片段重构为可重用元素(帮助程序/函数/部分/构建器/等)?

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.

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.

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

or at least like this:

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?

Alternatively, maybe you can describe how do you refactor HAML snippets to reusable elements (helpers/functions/partials/builders/etc)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

海螺姑娘 2024-10-28 04:13:26

来自 参考

def render_haml(code)
    engine = Haml::Engine.new(code)
    engine.render
end

这会启动一个新的 Haml 引擎并渲染它。

From the reference:

def render_haml(code)
    engine = Haml::Engine.new(code)
    engine.render
end

This initiates a new Haml engine and renders it.

幽梦紫曦~ 2024-10-28 04:13:26

如果您想要的只是一个可重用的小片段的方法,那么带有局部变量的部分怎么样? http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

If all you are after is a method for small reusable snippets, how about partials with local variables? http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

落日海湾 2024-10-28 04:13:26

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

Haml now has a capture_haml method that you can use to accomplish this.

  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>

Here is a link with more info on capture_haml:
http://haml.info/docs/yardoc/Haml/Helpers.html#capture_haml-instance_method

2024-10-28 04:13:26

我将heredoc用于此类目的:

  def view_helper
    Haml::Engine.new(<<~HAML).render
      .example
        #id ID
        .desc Description
    HAML
  end

这种方式在变量范围方面存在很多问题,因此,如上所述,更正确的方法是为此使用部分。

UPD1:这是关于如何解决范围问题的解决方案:

  def view_helper
    Haml::Engine.new(<<~HAML).render(self)
      .form
        = form_tag root_path do
          = submit_tag :submit
    HAML
  end

UPD2:更好的解决方案(建立在互联网上):

def render_haml(haml, locals = {})
  Haml::Engine.new(haml.strip_heredoc, format: :html5).render(self, locals)
end

def greeting
  render_haml <<-HAML
    .greeting
      Welcome to
      %span.greeting--location
        = Rails.env
  HAML
end

I used heredoc for such purposes:

  def view_helper
    Haml::Engine.new(<<~HAML).render
      .example
        #id ID
        .desc Description
    HAML
  end

This way has a lot of issues with a scope of variables, so, as mentioned above, the much more correct way is to use partials for this.

UPD1: here is a solution on how to solve issues with scope:

  def view_helper
    Haml::Engine.new(<<~HAML).render(self)
      .form
        = form_tag root_path do
          = submit_tag :submit
    HAML
  end

UPD2: even better solution(founded on the internet):

def render_haml(haml, locals = {})
  Haml::Engine.new(haml.strip_heredoc, format: :html5).render(self, locals)
end

def greeting
  render_haml <<-HAML
    .greeting
      Welcome to
      %span.greeting--location
        = Rails.env
  HAML
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文