-
Notifications
You must be signed in to change notification settings - Fork 1
StringBuilder
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.
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.
echo sb()
->add('First sentence.')
->add('Second sentence.')
->add('And a last one.');
Output:
First sentence. Second sentence. And a last one.
echo sb()
->add('Foo')
->nospace('bar');
Output:
Foobar
echo sb()
->html('<strong>')
->nospace('Text')
->html('</strong>');
Output:
<strong>Text</strong>
Note the
nospace('Text')
, to avoid adding a space before the text.
echo sb()->sf('Text with a %1$s.', 'placeholder');
Output:
Text with a placeholder.
echo sb()->bold('This is bold text.');
echo sb()
->add('First sentence.')
->eol()
->add('Second sentence.');
Output:
First sentence.
Second sentence.
echo sb()
->add('First sentence.')
->nl()
->add('Second sentence.');
Output:
First sentence.<br> Second sentence.
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>
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>
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()
);
echo sb()->t('This text will be translated.');
Output (if german translation is available):
Dieser Text wird übersetzt werden.
echo sb()->ul(array(
'Item One',
'Item Two'
));
Output:
<ul>
<li>Item One</li>
<li>Item Two</li>
</ul>
echo sb()->ol(array(
'Item One',
'Item Two',
'Item Three'
));
Output:
<ol>
<li>Item One</li>
<li>Item Two</li>
</ol>
echo sb()->quote('Quoted text');
Output:
"Quoted text"
echo sb()->code('Monospaced text');
Output:
<code>Monospaced text</code>
echo sb()->pre('Preformatted text');
Output:
<pre>Preformatted text</pre>
echo sb()->spanned('Some text', 'muted');
Output:
<span class="muted">Some text</span>
echo sb()->time();
Output:
14:32:11
These are canned texts that are often used in applications, and which support translation.
echo sb()->note();
Output:
Note:
Also available in bold text:
echo sb()->noteBold();
Output:
<strong>Note:</strong>
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.