title: How can I pass instance variables to a HAML template on the command line?
body: <h2>Background</h2>
  
  <p>I'm trying to test the formatting of some HAML templates <em>outside</em> of Rails. The idea is to pass in some instance variables on the command line or via an included Ruby file, rendering the template to standard output. I tried this several different ways without success, as outlined below.</p>
  
  <h2>Requiring a Ruby File</h2>
  
  <p>For example, given the following two files:</p>
  
  <ul>
  <li><p>HAML template: "test.haml"</p>
  
  <pre><code>!!!
  %h1 Testing HAML CLI
  %p= @bar
  %p= @baz
  </code></pre></li>
  <li><p>Ruby file: "test.rb"</p>
  
  <pre><code>@foo = 'abc'
  @bar = '123'
  </code></pre></li>
  </ul>
  
  <p>I would expect an invocation like <code>haml -r ./test test.haml</code> to return an interpolated HTML file on standard output, but it doesn't. Instead, I get just the HTML:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;h1&gt;Testing HAML CLI&lt;/h1&gt;
  &lt;p&gt;&lt;/p&gt;
  &lt;p&gt;&lt;/p&gt;
  </code></pre>
  
  <h2>Programmatic Attempt</h2>
  
  <p>Since this didn't work, I also tried to do this programmatically. For example:</p>
  
  <pre><code>#!/usr/bin/env ruby
  require 'haml'
  
  @foo = 'abc'
  @bar = '123'
  
  engine = Haml::Engine.new(File.read 'test.haml')
  puts engine.render
  </code></pre>
  
  <p>with exactly the same results, e.g. just the HTML with no variable interpolation.</p>
  
  <h2>Restating the Question</h2>
  
  <p>Clearly, something else is needed to get HAML to render the template with its associated variables. I would prefer to do this from the command line, either by passing arguments or including a file. How should I be invoking HAML from the command line to make it happen?</p>
  
  <p>If that's not possible for whatever reason, how should I invoke HAML programmatically to perform the interpolation without depending on Rails?</p>