Buffering allows you to capture printed or echoed data instead of outputting it right away.

Basic Buffer

To start buffering use the startBuffer() method. Then, to stop the buffer and get the captured output use the getCurrent() method.

echo 'First ';
$buffer = tr_buffer()->startBuffer();
echo 'Middle';
$middle = $buffer->getCurrent();
echo 'Last ';
echo $middle;

Will output,

Top Last Middle

Buffer Indexing

You can also index output to save chunks with the indexBuffer() method. You can then access the indexed buffer with the getBuffer() method.

$buffer = tr_buffer();

echo '<li>One</li>';

echo '<p>Two</p>';

echo '<ul>';
echo $buffer->getBuffer('one');
echo '</ul>';

echo $buffer->getBuffer('two');

Will output,