Module: Tiny::Buffering

Included in:
ActionViewAdditions, ErubisTemplating, HamlTemplating, Helpers
Defined in:
lib/tiny.rb

Overview

Buffering and capturing support.

Instance Method Summary collapse

Instance Method Details

#append(string) ⇒ String Also known as: text

Appends sanitized text to the content.

html_tag(:p) do
  text 'Foo &'
  text 'Bar'
end
# => <p>
Foo &amp;
Bar
</p>

Returns:



181
182
183
184
185
186
187
188
# File 'lib/tiny.rb', line 181

def append(string)
  string = Helpers.sanitize(string)
  if working_buffer
    working_buffer << string.gsub(/(?<!^|\n)\z/, "\n")
  else
    string
  end
end

#append!(content) ⇒ SafeString Also known as: text!

Appends non HTML-escaped text to the content.

html_tag(:p) do
  append! 'Foo & Bar'
  append! '<evil>'
end
# => <p>
Foo & Bar
<evil>
</p>

Shortcut for

append raw(content)

Returns:



208
209
210
# File 'lib/tiny.rb', line 208

def append!(content)
  append raw(content)
end

#raw(val) ⇒ SafeString

Returns content that won’t be HTML escaped when appended to content.

Returns:



217
218
219
# File 'lib/tiny.rb', line 217

def raw(val)
  SafeString.new val.to_s
end

#with_buffer(*args) {|*args| ... } ⇒ String

Buffers calls to markup generating methods.

Examples:

Not using #with_buffer Only the last tag is returned.

def my_helper
  html_tag(:span, 'Foo')
  html_tag(:span, 'Bar')
end
my_helper()
# => <span>Bar</span>

By using #with_buffer structured markup is generated.

def my_helper
  with_buffer do
    html_tag(:span, 'Foo')
    html_tag(:span, 'Bar')
  end
end
my_helper()
# => <span>Foo</span>
<span>Bar</span>

Parameters:

  • args (any)

    n number of arguments to be passed to block evaluation.

Yields:

  • (*args)

    Content block.

Returns:

See Also:



249
250
251
252
253
# File 'lib/tiny.rb', line 249

def with_buffer(*args)
  buffer_stack << ''
  yield(*args)
  buffer_stack.pop
end