简体   繁体   English

将局部语言中的局部语言和局部语言的haml渲染到html文件中

[英]Render haml with locals and partials within partials to html file

I need to create some html files and want to use my partials already done to the show view. 我需要创建一些html文件,并想使用已经在show视图中完成的部分设置。 I used to do this with markaby, but now i think that with haml the thing will be easier. 我曾经用markaby做到这一点,但现在我认为使用haml会更容易。 I'm trying to do: 我正在尝试做:

Haml::Engine.new(File.read("#{Rails.root}/app/views/metric_box_sets/_metric_box_set.html.haml"), 
:format => :html5, :ugly => true).render(Object.new,locals =>{:metric_box_set=>@metric_box_set})

in the partial that i'm using, i access metric_box_set several times, but also render other partials and give them other objects that are associated with this one. 在我正在使用的局部中,我多次访问metric_box_set,但还渲染了其他局部,并为它们提供了与此对象相关联的其他对象。 The problem is it's giving an error on the render method. 问题在于它在render方法上给出了错误。 There is some way to tell it that the render method it should use is the normal render method? 有某种方法可以告诉它应该使用的render方法是普通的render方法?

Tnks! Tnks!

I found other solution to do this. 我找到了其他解决方案来做到这一点。 Instead of using Haml::Engine, once we are already in rails and we can use render itself, i created a to_html function in the model and include the application helper to get the helper_methods that i used in the partial. 代替了使用Haml :: Engine,一旦我们已经可以使用渲染本身,我就在模型中创建了一个to_html函数,并包含了应用程序助手来获取我在局部函数中使用的helper_method。 Then used the render to print the result to a file: 然后使用渲染将结果打印到文件中:

def to_html
  ActionView::Base.send :include, ActionView::Helpers::ApplicationHelper

  File.open(File.join(Rails.root, "public", 'test.html'), "w") do |file|

    file.print ActionView::Base.new(Rails.configuration.paths["app/views"].first).render(
                :partial => 'metric_box_sets/metric_box_set', 
                :format => :html,
                :locals => { :metric_box_set => self}
              )
  end  
end

This way i can generate all html snippets that i needed based on the views that i already done. 这样,我可以根据已经完成的视图生成我需要的所有html代码段。

您输入错误- locals =>应该是locals =

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM