Rendering HAML partials from within HAML outside of Rails

前端 未结 5 579
后悔当初
后悔当初 2020-12-31 16:54

I\'m using HAML to generate some static html pages for a site, and I was wanting to split out common components into partials that I can include in multiple pages, just like

5条回答
  •  粉色の甜心
    2020-12-31 17:41

    All of the solutions seemed bulky for my purposes, though I'm trying to do basically the same thing. Here is a ruby script I wrote that does precious little - evaluates Haml with the option to stick in = partial "test.haml" anywhere you like. It does a trivial amount of logic to try and find a partial file.

    require 'haml'
    
    def find filename
      return filename if File.exists? filename
      path = File.dirname ARGV[0]
      filename = path+"/"+filename
      return filename if File.exists? filename
      throw "Could not find file."
    end
    
    def partial filename
      source = File.read(find(filename))
      engine = Haml::Engine.new(source)
      engine.render(binding)
    end
    
    puts partial ARGV[0]
    

    you execute it like so : ruby hamlize.rb input.haml > output.html

提交回复
热议问题