Rendering HAML partials from within HAML outside o

2019-02-06 19:52发布

问题:

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 in Rails. However I don't want to use the whole Rails stack to do this as it seems like overkill.

I've looked around on the Internet but haven't found anything, better than just doing something like:

Haml::Engine.new(IO.read("header.haml")).render

Is there a nicer way of including so-called partials from within HAML? An existing filter or command that I'm missing?

回答1:

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.



回答2:

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.



回答3:

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



回答4:

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.



回答5:

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


标签: ruby haml