map7
map7

Reputation: 5126

Rails render haml template to html

In rails I can use the following to turn erb into html

erb(filename_as_string)

I couldn't find a haml equivalent though. So I started to create a helper like so:

  def haml(file)
    lines = File.new(Rails.root.to_s + file).readlines.first
    engine = Haml::Engine.new(lines)
    engine.render
  end

This doesn't pass on any instance variables to my haml file though.

Is there an existing function which does this already? OR What should I add to my helper to pass on all instance variables which I create in my controller method?

Upvotes: 1

Views: 2304

Answers (1)

moritz
moritz

Reputation: 25757

Check out http://haml.info/docs/yardoc/Haml/Engine.html#render-instance_method. As you can see, it gets a new Object passed by default to be used as binding. So in your case, you probably want

engine.render(self)

also, for creating the engine, you can just

engine = Haml::Engine.new(File.read "#{Rails.root}#{file}")

Upvotes: 1

Related Questions