Sha256: aa51b2443d19ee633c013fb9dea577ba22b5559a86c13504150ecaecdf03d8fc

Contents?: true

Size: 1.94 KB

Versions: 21

Compression:

Stored size: 1.94 KB

Contents

module Padrino
  module Helpers
    module OutputHelpers
      class AbstractHandler
        attr_reader :template, :output_buffer

        def initialize(template)
          @template = template
          @output_buffer = template.instance_variable_get(:@_out_buf)
        end

        ##
        # Returns true if the block given is of the handler's template type; false otherwise.
        #
        # @example
        #   @handler.engine_matches?(block) => true
        #
        def engine_matches?(block)
        end

        ##
        # Captures the html from a block of template code for this handler.
        #
        # This method is called to capture content of a block-loving helpers in templates.
        # Haml has a special method to do this, for Erb and Slim we save original buffer,
        # call the block and then restore the buffer.
        #
        # @example
        #   @handler.capture_from_template(&block) => "...html..."
        #
        def capture_from_template(*args, &block)
          self.output_buffer, _buf_was = SafeBuffer.new, self.output_buffer
          raw = yield(*args)
          captured = template.instance_variable_get(:@_out_buf)
          self.output_buffer = _buf_was
          engine_matches?(block) && !captured.empty? ? captured : raw.to_s
        end

        ##
        # Outputs the given text to the template.
        #
        # This method is called when template uses block-aware helpers. For Slim and Haml such
        # helpers just return output to use with `=`. For Erb this method is implemented in
        # ErbHandler by concatenating given text to output buffer.
        #
        # @example
        #   @handler.concat_to_template("This will be output to the template buffer")
        #
        def concat_to_template(text="", context=nil)
          text
        end

        protected

        def output_buffer=(val)
          template.instance_variable_set(:@_out_buf, val)
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
padrino-helpers-0.16.0.pre3 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.16.0.pre2 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.15.3 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.15.2 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.15.1 lib/padrino-helpers/output_helpers/abstract_handler.rb
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/padrino-helpers-0.13.3.4/lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.15.0 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.14.4 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.14.3 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.14.2 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.13.3.4 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.14.1.1 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.14.1 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.14.0.2 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.14.0.1 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.14.0 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.14.0.rc2 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.14.0.rc1 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.13.3.3 lib/padrino-helpers/output_helpers/abstract_handler.rb
padrino-helpers-0.13.3.2 lib/padrino-helpers/output_helpers/abstract_handler.rb