Skip to content

StringBuilder

Mistralys edited this page Apr 29, 2021 · 9 revisions

The string builder is used to easily concatenate strings in an object oriented way. It is optimized for outputting HTML, with common snippets, and ties into the Application Localization package for translating texts.

The basic principle is that every bit of text is automatically prepended with a space (except the first), to avoid having to add them manually. Each method returns the same StringBuilder instance, allowing them to be chained.

Examples

Adding bits of text

echo sb()
  ->add('First sentence.')
  ->add('Second sentence.')
  ->add('And a last one.');

Adding text via sprintf

echo sb()
  ->sf('Text with a %1$s.', 'placeholder');

Adding bold text

echo sb()->bold('This is bold text.');

Add a linebreak character

echo sb()->eol();

Add a <br> tag

echo sb()->nl();

Add a paragraph

Default is to add two <br> tags:

echo sb()->para();

To add a <p> tag instead, specify some content:

echo sb()->para('Some text here');

Adding a link tag

echo sb()->link('AppUtils', 'https://github.com/Mistralys/application-utils');

Opening the link in a new tab/window:

echo sb()->link('AppUtils', 'https://github.com/Mistralys/application-utils', true);

Adding translated text

echo sb()->t('This text will be translated.');

New here?

Have a look at the overview for a list of all helper classes available in the package.

Table of contents

Find the current page in the collapsible "Pages" list above, and expand the page, to view a table of contents.

Clone this wiki locally