我对ruby还是很陌生,并且正在构建一个前端样式指南,该样式指南具有html片段,我希望将这些片段作为haml呈现到pre标签中。我正在为中间人构建一个助手,并且已经弄清楚了如何读取HTML文件并输出其内容。现在,我想将html转换为haml并输出。
环顾四周似乎是我想要使用的html2haml gem,尽管该gem上的文档似乎只涵盖了在命令行上使用它,而我正在尝试将此功能添加到帮助程序中。
到目前为止,这是我所需要的帮助
helpers do
def render_snippet(page)
p1 = ("<pre><code>").html_safe
p2 = File.read("source/"+"#{page}")
p3 = ("</code></pre>").html_safe
p0+p1+p2+p3
end
end
这是我使用助手的方式
= render_snippet "partials/examples/typography/elements.html"
为了回答您的问题,这是如何使您的助手在终端外壳命令之外使用html2haml gem
# some_view.html.erb
<%= render html_2_haml("home/my_partial.html") %>
# app/helpers/application_helper.rb
module ApplicationHelper
def html_2_haml(path)
file_name = path.split("/").last
path_with_underscore = path.gsub(file_name, "_#{file_name}")
system "html2haml app/views/#{path_with_underscore} app/views/#{path_with_underscore}.haml"
"#{path}.haml"
end
end
现在我想说的是,这绝对不会在生产环境中起作用(因为它是动态创建新文件,并且像Heroku这样的托管服务将不允许这样做),但是如果您只是为此做自己的开发助手,那么也许这对您有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句