Class: Padrino::Helpers::OutputHelpers::AbstractHandler

Inherits:
Object
  • Object
show all
Defined in:
padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb

Direct Known Subclasses

ErbHandler, HamlHandler, HamlitHandler, SlimHandler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ AbstractHandler

Returns a new instance of AbstractHandler.



7
8
9
10
# File 'padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb', line 7

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

Instance Attribute Details

#output_bufferObject

Returns the value of attribute output_buffer.



5
6
7
# File 'padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb', line 5

def output_buffer
  @output_buffer
end

#templateObject (readonly)

Returns the value of attribute template.



5
6
7
# File 'padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb', line 5

def template
  @template
end

Instance Method Details

#capture_from_template(*args, &block) ⇒ Object

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.

Examples:

@handler.capture_from_template(&block) => "...html..."


31
32
33
34
35
36
37
# File 'padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb', line 31

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

#concat_to_template(text = "", context = nil) ⇒ Object

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.

Examples:

@handler.concat_to_template("This will be output to the template buffer")


49
50
51
# File 'padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb', line 49

def concat_to_template(text="", context=nil)
  text
end

#engine_matches?(block) ⇒ Boolean

Returns true if the block given is of the handler’s template type; false otherwise.

Examples:

@handler.engine_matches?(block) => true

Returns:

  • (Boolean)


18
19
# File 'padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb', line 18

def engine_matches?(block)
end