Rendering HAML partials from within HAML outside of Rails

前端 未结 5 546
后悔当初
后悔当初 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:24

    I had the same problem. I needed to generate some html snippets to optimize the server response, once this snippets have a lot of calculations(for drawing graphs) and every time a user do a request it was doing all the stuff unnecessarily.

    Once i have the views partitioned in several haml partials, i wanted to reuse this to generate the html files.

    My first approach was trying with the Haml engine, but once i have partials rendering other partials and some functions of the application helper, it didn't worked. Finality i found a solution that passed by creating a function in the models that i wanted to create an html file ( to_html ) Here is it:

    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 => 'partial_folder/partial', 
                    :format => :html,
                    :locals => { :model => self}
                  )
      end  
    end
    

    This way, i was able to use the already done partials, to print the html files. This worked for me, maybe it can help you also.

    0 讨论(0)
  • 2020-12-31 17:33

    I finally found what I was looking for, so I thought I'd share my find with you. you can simply use the same syntax you use for haml in the first place - http://www.semicomplete.com/blog/geekery/partials-in-haml-in-sinatra.html

    /views/file.haml

    %h3 Title
    %div= haml :content, :layout => false
    

    /views/content.haml

    %p your content here
    
    0 讨论(0)
  • 2020-12-31 17:36

    It's best to combine haml & sass with a tool for building static websites. Here's some links:

    • StaticMatic
    • Serve
    • Haml Jekyll -- a fork of Jekyll that supports haml.
    • Middleman

    I'm using jekyll for my blog, but if you're not building a blog it's probably not appropriate for your needs.

    0 讨论(0)
  • 2020-12-31 17:36

    Darn, you're right – there isn't a built-in way. I've used helpers with the haml command line, but always ones whose output was already HTML formatted.

    My best suggestion is to write a partial() method and require it. It looks like you have already started down this path. I would suggest anyone writing such a function consider keeping the original binding around in some way. Haml::Helpers#capture_hame looks to be the easiest way to make this happen.

    If execution speed is an issue, it might also be a good idea to cache some of the parsed template in the same way that Merb does it.

    If someone does get some code working, please put it up on GitHub and make a comment here.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题