title: Rails 2.2.2: Render an template with yield, inside a model
body: <p>I'n trying to write out an html file onto our filesystem, so it can be zipped up and sent out, along with some other stuff.</p>
  
  <p>I have an erb template I want to use, in our <code>app/views/layouts</code> folder.  That template has a standard <code>&lt;%= yield %&gt;</code> call in it, and in this instance I have a bunch of pre-generated html, as a string in my method, that I want to replace yield with.</p>
  
  <p>So, I need to do something like this (pseudocode):</p>
  
  <pre><code>string = "&lt;div&gt;This is some &lt;strong&gt;html&lt;/strong&gt;&lt;/div&gt;"
  erb_template = "app/views/layouts/my_layout.html.erb"
  output_filename = "/tmp/output.html"
  
  #something like this...
  render_to_file(erb_template, output_filename) do 
    string
  end
  </code></pre>
  
  <p>It might be simpler for me to try to just gsub out <code>&lt;%= yield %&gt;</code> and replace it with my text, but that feels like a cop out...</p>
  
  <p>Btw, if it's easier to get the result as a string, eg using <code>render_to_string</code>, and then write it to a file myself, that's fine.  It's the evaluation of the template with the yield inside that I don't get...</p>
  
  <p>EDIT:  I've tried changing <code>&lt;%= yield %&gt;</code> to <code>&lt;%= @content %&gt;</code>, and then doing this, but they both raise a "protected method call" error:</p>
  
  <pre><code>c = ActionController::Base.new()
  html = c.render_to_string :template =&gt; erb_template, :layout =&gt; false, :locals =&gt; {:content =&gt; string}
  
  c = ApplicationController.new()
  html = c.render_to_string :template =&gt; erb_template, :layout =&gt; false, :locals =&gt; {:content =&gt; string}
  </code></pre>