Skip to content

StringBuilder

Mistralys edited this page Jan 7, 2022 · 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.

Adding custom methods

All methods are documented to return $this, so extending the class is IDE friendly.

For type checks, use the interface StringBuilder_Interface, which will match both the original class and all extending classes.

Examples

Adding bits of text

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

Output:

First sentence. Second sentence. And a last one.

Adding a bit of text without space

echo sb()
  ->add('Foo')
  ->nospace('bar');

Output:

Foobar

Adding a bit of HTML code

echo sb()
  ->html('<strong>')
  ->nospace('Text')
  ->html('</strong>');

Output:

<strong>Text</strong>

Note the nospace('Text'), to avoid adding a space before the text.

Adding text via sprintf

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

Output:

Text with a placeholder.

Adding bold text

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

Add a linebreak character

echo sb()
  ->add('First sentence.')
  ->eol()
  ->add('Second sentence.');

Output:

First sentence.
Second sentence.

Add a <br> tag

echo sb()
  ->add('First sentence.')
  ->nl()
  ->add('Second sentence.');

Output:

First sentence.<br> Second sentence.

Add a paragraph

Default is to add two <br> tags:

echo sb()
  ->add('First sentence.')
  ->para()
  ->add('Second sentence.');

Output:

First sentence.<br><br> Second sentence.

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

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

Output:

<p>Some text here</p>

Adding a link tag

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

Output:

<a href="https://github.com/Mistralys/application-utils">AppUtils</a>

Opening the link in a new tab/window:

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

Output:

<a href="https://github.com/Mistralys/application-utils" target="_blank">AppUtils</a>

Linking text in a sentence

When linking, you may sometimes want to leave the link text in to make the translator's job easier. In this case, the link can be split into the opening and closing tags:

echo t(
    'Check out our %1$sdocumentation%2$s',
    sb()->linkOpen('https://mistralys.com'),
    sb()->linkClose()
);

Adding translated text

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

Output (if german translation is available):

Dieser Text wird übersetzt werden.

Add an unordered list

echo sb()->ul(array(
    'Item One',
    'Item Two'
));

Output:

<ul>
  <li>Item One</li>
  <li>Item Two</li>
</ul>

Add an ordered list

echo sb()->ol(array(
    'Item One',
    'Item Two',
    'Item Three'
));

Output:

<ol>
  <li>Item One</li>
  <li>Item Two</li>
</ol>

Add double-quoted text

echo sb()->quote('Quoted text');

Output:

&quot;Quoted text&quot;

Add inline code

echo sb()->code('Monospaced text');

Output:

<code>Monospaced text</code>

Add a preformatted text block

echo sb()->pre('Preformatted text');

Output:

<pre>Preformatted text</pre>

Add a <span> tag with CSS classes

echo sb()->spanned('Some text', 'muted');

Output:

<span class="muted">Some text</span>

Add the current time

echo sb()->time();

Output:

14:32:11

Add a canned translatable text

These are canned texts that are often used in applications, and which support translation.

Note

echo sb()->note();

Output:

Note:

Also available in bold text:

echo sb()->noteBold();

Output:

<strong>Note:</strong>

Hint

echo sb()->hint();

Output:

Hint:

Also available in bold text:

echo sb()->hintBold();

Output:

<strong>Hint:</strong>

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