# awesome.php
<html>
<head></head>
<body>
<h1>Hello <?= $name ?> !</h1>
</body>
</html>
use Colorium\Template\Templater;
$templater = new Templater;
echo $templater->render('awesome', ['name' => 'you']); // Hello you !
Setup root directory :
$templater->directory = __DIR__ . '/../views/';
Setup file extension (default: .php
) :
$templater->suffix = '.phtml';
An helper is an inner sandboxed function, only usable in template.
$templater->helpers['hi'] = function($name)
{
return 'Hi ' . $name . ' !';
};
# awesome.php
<html>
<head></head>
<body>
<h1><?= self::hi($name) ?></h1>
</body>
</html>
You can set a layout for the current template (and pass some data) :
# awesome.php
<?php self::layout('mylayout', ['title' => 'Awsome Page']) ?>
<h1>Hello <?= $name ?> !</h1>
In the layout file, define where to place the template content :
# mylayout.php
<html>
<head>
<title><?= $title ?></title>
</head>
<body>
<?= self::content() ?>
</body>
</html>
You can define accessible blocks in the layout using block($name)
:
# mylayout.php
<html>
<head></head>
<body>
<nav>
<?php self::block('breadcrumb') ?>
Homepage
<?php self::end() ?>
</nav>
<?= self::content() ?>
</body>
</html>
And change it from the template using rewrite($name)
:
# awesome.php
<?php self::layout('mylayout') ?>
<?php self::rewrite('breadcrumb') ?>
Homepage > Awesome
<?php self::end() ?>
<h1>Hello !</h1>
Result :
# mylayout.php
<html>
<head>
<title></title>
</head>
<body>
<nav>
Homepage > Awesome
</nav>
<h1>Hello !</h1>
</body>
</html>
composer require colorium/templating