Formatting data - Level-2/Transphporm GitHub Wiki
Transphporm supports formatting of data as its output. The general syntax for formatting is this:
h1 {content: "content of element"; format: [NAME-OF-FORMAT] [OPTIONAL ARGUMENT OF FORMAT];}Transphporm currently supports the following formats for strings:
- uppercase
- lowercase
- titlecase
- nl2br
- html
Examples:
$xml = '
<h1> </h1>
';
$tss = 'h1 {content: "TeSt"; format: uppercase}';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;Prints:
<h1>TEST</h1>$xml = '
<h1> </h1>
';
$tss = 'h1 {content: "TeSt"; format: lowercase}';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;Prints:
<h1>test</h1>$xml = '
<h1> </h1>
';
$tss = 'h1 {content: "test"; format: titlecase}';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;Prints:
<h1>Test</h1>$xml = '
<p></p>
';
$tss = 'p {content: "test1\ntest2"; format: nl2br}';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;Prints:
<p>test1<br/>test2</p>Whenever you use a content string, the content is escaped by replacing HTML entities unless you set format: html.
$xml = '<p></p>';
$tss = 'p {content: "<h1>My Title</h1>"; format: html}';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;Prints:
<p><h1>test1</h1></p>Transphporm supports formatting numbers to a number of decimal places using the decimal format. You can specify the number of decimal places:
$xml = '
<h1> </h1>
';
$tss = 'h1 {content: "11.234567"; format: decimal 2}';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;Prints:
<h1>1.23</h1>Transphporm supports formatting currency to a locale-specific number of decimal places, digit separators and a currency symbol in the locale position.
$xml = '
<h1> </h1>
';
$tss = 'h1 {content: "11.234567"; format: currency}';
$template = new \Transphporm\Builder($xml, $tss);
$template->setLocale('enUS');
echo $template->output()->body . PHP_EOL;
$template->setLocale('enGB');
echo $template->output()->body . PHP_EOL;
$template->setLocale('frFR');
echo $template->output()->body . PHP_EOL;
$template->setLocale('deDE');
echo $template->output()->body . PHP_EOL;Prints:
<h1>$1,234.57</h1>
<h1>£1,234.57</h1>
<h1>1 234,57€</h1>
<h1>1,234.57€</h1>