How can I use Rails Erubis templating standalone?

Hi,

I am trying to write an RSpec 2 extension, which lets me test my
templating view logic standalone without loading the “full rails”,
including controller, routes etc. etc.

I simply want to be able to take a String in the form of a valid ERB
template and render it, where the @outputbuffer is on (for delayed
handling of blocks), and then set any values in the view context and
extend it with whatever helper modules that suit the particular
context.

The ActionView::TestCase, is a bit too “heavy” (includes too much
outside of basic view functionality), so I have been looking at
ActionView::Template.

The #render method takes arguments view and a locals, which I think is
a Hash.

Is this what I should be aiming to use? Any examples of this kind of
usage outside Rails?

Kristian

I found the file ‘actionpack/template/template_test.rb’, which shows
how to run simple templates:

def test_basic_template
@template = new_template
assert_equal “Hello”, render
end

def test_locals
@template = new_template("<%= my_local %>")
assert_equal “I’m a local”, render(:my_local => “I’m a local”)
end

def test_restores_buffer
@template = new_template
assert_equal “Hello”, render
assert_equal “original”, @obj.my_buffer
end

But not sure how to make it work with the @output_buffer for blocks,
fx when using view helpers that take blocks and such?

<% posts.each do |post| %>
hello <%= post %>
<% end %>

Where should I look for this? Is this functionality isolated and
demonstrated with test cases somewhere?

Thanks.

After a lot of experimentation and reverse engineering the Rails 3
ActionView code, I figured out this solution:

Hope it can help others with a similar need :wink:

Find my answer below: