require 'erbal'

if ActiveSupport.const_defined?('SafeBuffer')

  class ErbalTemplateHandler < ActionView::TemplateHandler
    include ActionView::TemplateHandlers::Compilable
    def compile(template)
      ::Erbal.new("<% __in_erb_template=true %>#{template.source}", {:buffer => '@output_buffer', :buffer_initial_value => 'ActiveSupport::SafeBuffer.new'}).parse
    end
  end

else

  class ErbalTemplateHandler < ActionView::TemplateHandler
    include ActionView::TemplateHandlers::Compilable
    def compile(template)
      ::Erbal.new("<% __in_erb_template=true %>#{template.source}", {:buffer => '@output_buffer'}).parse
    end
  end

end
require 'erbal'

class ErbalTemplateHandler < ActionView::TemplateHandler
  include ActionView::TemplateHandlers::Compilable
  def compile(template)
    ::Erbal.new("<% __in_erb_template=true %>#{template.source}", {:buffer => '@output_buffer'}).parse
  end
end
--- 
+++ 
@@ -1,8 +1,21 @@
 require 'erbal'
 
-class ErbalTemplateHandler < ActionView::TemplateHandler
-  include ActionView::TemplateHandlers::Compilable
-  def compile(template)
-    ::Erbal.new("<% __in_erb_template=true %>#{template.source}", {:buffer => '@output_buffer'}).parse
+if ActiveSupport.const_defined?('SafeBuffer')
+
+  class ErbalTemplateHandler < ActionView::TemplateHandler
+    include ActionView::TemplateHandlers::Compilable
+    def compile(template)
+      ::Erbal.new("<% __in_erb_template=true %>#{template.source}", {:buffer => '@output_buffer', :buffer_initial_value => 'ActiveSupport::SafeBuffer.new'}).parse
+    end
   end
+
+else
+
+  class ErbalTemplateHandler < ActionView::TemplateHandler
+    include ActionView::TemplateHandlers::Compilable
+    def compile(template)
+      ::Erbal.new("<% __in_erb_template=true %>#{template.source}", {:buffer => '@output_buffer'}).parse
+    end
+  end
+
 end
Set the buffer initial value to a SafeBuffer instance for versions of Rails that use it
