views:

141

answers:

2

I've installed HAML into my project and it is working like a charm - the templates are beeing rendered without a problem. My question is how can I do the rendering on the command line, by using HAML program. That would be super for debugging purposes, meantime while I try to compile HAML file I get the error on first Rails related Ruby code to be found:

% cat app/views/dashboard/index.html.haml
- title "Home"
%p
  Lorem ipsum dolor sit amet...

% haml app/views/dashboard/index.html.haml
Exception on line 1: undefined method `title' for #<Object:0xb73283b0>
  Use --trace for backtrace.

Page is rendered fine returned correctly through the webserver.

+1  A: 

I'd say that you want to take a look at the haml engine

template = File.read('templates/really_cool_template.haml')
haml_engine = Haml::Engine.new(template)
output = haml_engine.render
puts output

edit: Then put that into a rake task to load the rails environment. Once it's done you can pass all the parameters you want to your template

marcgg
A: 

You can use the --check flag on the Haml executable to check for syntax without evaluating the Ruby code:

% haml --check app/views/dashboard/index.html.haml

For future reference, the --help flag will print out all options for the executable.

nex3
This functionality is quite useless for me. If theres a syntax error, then Rails will display it in the browser during developement. What I need is to check the logic of the template and see whats the output.
mdrozdziel
I'm sorry... normally when one refers to the syntax of a template, one means the surface syntax, not the underlying semantics of the code.In order to run the full template, you need to load the entire Rails stack and will likely need to have an actual HTTP request instantiated. This is not something that's well-suited for the command line.
nex3