module Haml
  class SafeErubisTemplate < Tilt::ErubisTemplate

    def initialize_engine
    end

    def prepare
      @options.merge! :engine_class => ActionView::Template::Handlers::Erubis
      super
    end

    def precompiled_preamble(locals)
      [super, "@output_buffer = ActionView::OutputBuffer.new;"].join("\n")
    end

    def precompiled_postamble(locals)
      [super, '@output_buffer.to_s'].join("\n")
    end
  end
end
module Haml

  class ErubisTemplateHandler < ActionView::Template::Handlers::Erubis

    def initialize(*args, &blk)
      @newline_pending = 0
      super
    end
  end

  class SafeErubisTemplate < Tilt::ErubisTemplate

    def initialize_engine
    end

    def prepare
      @options.merge! :engine_class => Haml::ErubisTemplateHandler
      super
    end

    def precompiled_preamble(locals)
      [super, "@output_buffer = ActionView::OutputBuffer.new;"].join("\n")
    end

    def precompiled_postamble(locals)
      [super, '@output_buffer.to_s'].join("\n")
    end
  end
end
Use custom Erubis handler subclass for Erb filter
Use custom Erubis handler subclass for Erb filter

The Rails Erubis handler has added code to reduce the number of method
calls when adding newlines (see
https://github.com/rails/rails/commit/bd308a0d02c4a6459c05770a564405986a663f47).

This uses a new instance variable, which is initialised in the
add_preamble method. Tilt explicitly ignores the preamble from Erubis,
using its own, so this variable is never initialised. This causes
failures with the Erb filter when this variable is referenced.

By using our own custom subclass the variable can be initialised in the
classes initialize method, preventing failures.

Fixes #669
