【问题标题】:How to render html file as haml如何将html文件呈现为haml
【发布时间】:2015-12-17 12:45:49
【问题描述】:

我对 ruby​​ 还很陌生,正在构建一个包含 html sn-ps 的前端样式指南,我想将其作为 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"

【问题讨论】:

    标签: html ruby haml helper


    【解决方案1】:

    为了回答您的问题,您可以通过以下方式在终端 shell 命令之外创建一个帮助器来使用 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 这样的托管服务不允许这样做)但是如果你只是让自己成为这个的开发助手 - 并且- 那也许这对你有帮助。

    【讨论】:

      【解决方案2】:

      我最终在这方面做了更多工作,最终得到了以下结果:

      def render_html2haml(file)
        templateSource = preserve(File.read("source/"+"#{file}"))
        haml = Html2haml::HTML.new(templateSource, {:erb => nil})
        content_tag(:pre, content_tag(:code, haml.render))
      end
      

      【讨论】:

        猜你喜欢
        • 2011-03-10
        • 2010-09-08
        • 1970-01-01
        • 2011-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多