Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you pass ruby objects to haml during a render?

Tags:

ruby

haml

I'm trying to create a haml template that uses some data from my ruby app to fill in some content. Is it possible to pass arguments into haml to make it render correctly? Here's how I'm getting my haml template and rendering it:

template = File.open('path/to/template.haml')
html = Haml::Engine.new(template.read).render

So, is it possible to pass objects from the local Ruby script into the template file so that they the page is rendered correctly? Or, can I have the haml file pull in objects?

If this doesn't work, my only other idea is building up the template as a local string which seems more tedious to me. So, is there a different coding pattern that is more efficient for this job?

like image 811
Nathan Avatar asked Mar 06 '12 01:03

Nathan


1 Answers

Yes,

Check the docs: http://haml.info/docs/yardoc/Haml/Engine.html#render-instance_method

  • (String) render(scope = Object.new, locals = {}, &block)

pass them as locals

like image 144
j_mcnally Avatar answered Oct 20 '22 18:10

j_mcnally