Class: Twig::OutputBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/twig/output_buffer.rb

Overview

Anything passed through the Twig OutputBuffer is already being checked for escaping issues, so we can mark everything html_safe here. When being used with Rails, we just pass everything through this buffer into the Rails safe buffer so we don’t need a bunch of html_safe calls everywhere.

Instance Method Summary collapse

Constructor Details

#initialize(decorated = nil) ⇒ OutputBuffer

Returns a new instance of OutputBuffer.



10
11
12
13
# File 'lib/twig/output_buffer.rb', line 10

def initialize(decorated = nil)
  @decorated = decorated
  @buffer = +''
end

Instance Method Details

#append=(string) ⇒ Object



15
16
17
# File 'lib/twig/output_buffer.rb', line 15

def append=(string)
  self.safe_append = string
end

#safe_append=(string) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/twig/output_buffer.rb', line 19

def safe_append=(string)
  if @decorated
    @decorated.safe_append = string
  else
    @buffer << string.to_s.html_safe
  end
end

#to_sObject



27
28
29
# File 'lib/twig/output_buffer.rb', line 27

def to_s
  @decorated || @buffer
end