PHP code example of level-2 / transphporm
1. Go to this page and download the library: Download level-2/transphporm library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
level-2 / transphporm example snippets
<ul>
foreach ($users as $user) :
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
<ul>
{users}
<li>{user.name}</li>
{/users}
</ul>
<ul>
<li>User name</li>
</ul>
h1 {content: "My Title";}
$xml = '<h1>Original Title</h1>';
$tss = 'h1 {content: "Replaced Title"; }';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;
<h1>Replaced Title</h1>
//Load files instead of strings, the base path is the current working directory (getcwd())
$template = new \Transphporm\Builder('template.xml', 'stylesheet.tss');
$axel = new \Axel\Axel;
$axel->addModule(new \Axel\Module\PSR0('./path/to/Transphporm/src', '\\Transphporm'));
$xml = '<h1>Original Title</h1>';
$data = 'My Title!';
$tss = 'h1 {content: data(); }';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output($data)->body;
<h1>My Title!</h1>
$data = new stdclass;
$data->title = 'My Title!';
$data->description = 'Description of the page...';
$xml = '
<h1>Example Title</h1>
<p>Example Description</p>
';
$tss = '
h1 {content: data(title);}
p {content: data(description);}
';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output($data)->body;
<h1>My Title!</h1>
<p>Description of the page....</p>
$xml = '<h1>Original Title</h1>';
$data = 'My Title!'
$tss = 'h1 {content: "Title: ", data(); }';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output($data)->body;
$users = [];
$user = new stdclass;
$user->name = 'Tom';
$user->email = '[email protected] ';
$users[] = $user;
$user = new stdclass;
$user->name = 'Scott';
$user->email = '[email protected] ';
$users[] = $user;
$xml = '<ul>
<li>Name</li>
</ul>';
$tss = '
ul li {repeat: data(users); content: iteration(name);}
';
$data = ['users' => $users];
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output($data)->body;
<ul>
<li>Tom</li>
<li>Scott</li>
</ul>
$xml = '<ul>
<li>
<h3>Name</h3>
<span>email</span>
</li>
</ul>';
$tss = '
ul li {repeat: data(users);}
ul li h3 {content: iteration(name);}
ul li span {content: iteration(email);}
';
$data = ['users' => $users];
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output($data)->body;
<ul>
<li>
<h3>Tom</h3>
<span>[email protected] </span>
</li>
<li>
<h3>Scott</h3>
<span>[email protected] </span>
</li>
</ul>
$xml = '<ul>
<li>
<h3>Name</h3>
<span>email</span>
</li>
</ul>';
$tss = '
ul li {repeat: data(users);}
ul li h3 {content: iteration(name);}
ul li span {display: none}
';
$data = ['users' => $users];
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output($data)->body;
<ul>
<li>
<h3>Tom</h3>
</li>
<li>
<h3>Scott</h3>
</li>
</ul>
element:iteration[name=value] {}
$users = [];
$user = new stdclass;
$user->name = 'Tom';
$user->email = '[email protected] ';
$user->type = 'Admin';
$users[] = $user;
$user = new stdclass;
$user->name = 'Scott';
$user->email = '[email protected] ';
$user->type = 'Standard';
$users[] = $user;
$user = new stdclass;
$user->name = 'Jo';
$user->email = '[email protected] ';
$user->type = 'Standard';
$users[] = $user;
$xml = '
<ul>
<li>
<h3>Name</h3>
<span>email</span>
</li>
</ul>';
$tss = '
ul li {repeat: data(users);}
ul li:iteration[type='Admin'] {display: none;}
ul li h3 {content: iteration(name);}
ul li span {content: iteration(email);}
';
$data = ['users' => $users];
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output($data)->body;
<ul>
<li>
<h3>Scott</h3>
<span>[email protected] </span>
</li>
<li>
<h3>Jo</h3>
<span>[email protected] </span>
</li>
</ul>
$users = [];
$user = new stdclass;
$user->name = 'Tom';
$user->email = '[email protected] ';
$users[] = $user;
$user = new stdclass;
$user->name = 'Scott';
$user->email = '[email protected] ';
$users[] = $user;
$xml = '
<ul>
<li>
<h3>Name</h3>
<a href="mailto:email">email</a>
</li>
</ul>';
$tss = '
ul li {repeat: data(users);}
ul li a {content: iteration(email);}
ul li a:attr(href) {content: "mailto:", iteration(email);}
';
$data = ['users' => $users];
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output($data)->body;
<ul>
<li>
<h3>Tom</h3>
<a href="mailto:[email protected] ">[email protected] </a>
</li>
<li>
<h3>Scott</h3>
<a href="mailto:[email protected] ">[email protected] </a>
</li>
</ul>
$xml = '
<h1 class="foo">bar</h1>
';
$tss = 'h1 {content: attr(class);}';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;
<h1 class="foo">foo</h1>
$xml = '<html><div>Foo</div></html>';
$tss = 'html:header[location] {content: "/redirect-url"; }';
$template = new \Transphporm\Builder($xml, $tss);
print_r($template->output());
Array (
'body' => '<html><div>foo</div></html>',
'headers' => Array (
Array (
[0] => 'location',
[1] => '/redirect-url'
)
)
)
foreach ($template->output()->headers as $header) {
header($header[0] . ': ' . $header[1]);
}
class Model {
public function getProduct() {
return false;
}
}
$tss = 'html:data[getProduct='']:header[status] {content: '404'}
$xml = '<html></html>';
$data = new Model;
$template = new \Transphporm\Builder($xml, $tss);
$output = $template->output($data);
print_r($output->headers)
Array (
[0] => 'status',
[1] => '404'
)
foreach ($template->output()->headers as $header) {
if ($header[0] === 'status') http_response_code($header[1]);
else header($header[0] . ': ' . $header[1]);
}
$xml = '
<h1> </h1>
';
$tss = 'h1 {content: "TeSt"; format: uppercase}';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;
$xml = '
<h1> </h1>
';
$tss = 'h1 {content: "TeSt"; format: lowercase}';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;
$xml = '
<h1> </h1>
';
$tss = 'h1 {content: "test"; format: titlecase}';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;
$xml = '
<h1> </h1>
';
$tss = 'h1 {content: "11.234567"; format: decimal 2}';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;
$template = new \Transphporm\Builder($xml, $tss);
$template->setLocale('enGB');
$xml = '
<div> </div>
';
$tss = 'div {content: "2015-12-22"; format: date}';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;
$xml = '
<div> </div>
';
$tss = 'div {content: "2015-12-22"; format: date "jS M Y"}';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;
$xml = '
<div> </div>
';
$tss = 'div {content: "2015-12-22 14:34"; format: time}';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;
$xml = '
<h1> </h1>
<div> </div>
';
$tss = "
@import 'imported.tss';
div {content: 'From main tss'}
";
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output()->body;
$cache = new \SimpleCache\SimpleCache('/tmp');
$template = new \Transphporm\Builder($xml, $tss);
$template->setCache($cache);
echo $template->output($data)->body;
$template = new \Transphporm\Builder('layout.xml', 'home.tss');
echo $template->output()->body;
$template = new \Transphporm\Builder('layout.xml', 'about.tss');
echo $template->output()->body;
//Home template:
$template = new \Transphporm\Builder('layout.xml', 'page.tss');
$template->output(['title' => 'My Website', 'page' => 'home.xml'])->body;
//About template:
$template = new \Transphporm\Builder('layout.xml', 'page.tss');
$template->output(['title' => 'About Me', 'page' => 'about.xml'])->body;