PHP code example of bugo / antlers-php
1. Go to this page and download the library: Download bugo/antlers-php 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/ */
bugo / antlers-php example snippets
use Bugo\Antlers\Engine;
$engine = new Engine();
echo $engine->render('Hello, {{ name }}!', ['name' => 'World']);
// → Hello, World!
// Callable
$engine->addModifier('money', function(mixed $value, array $params, array $context): string {
$currency = $params[0] ?? 'USD';
return number_format((float) $value, 2) . ' ' . $currency;
});
// {{ price | money:EUR }}
// Class
use Bugo\Antlers\Modifiers\ModifierInterface;
class ExcerptModifier implements ModifierInterface
{
public function modify(mixed $value, array $params, array $context): mixed
{
$length = (int) ($params[0] ?? 150);
return mb_substr(strip_tags((string) $value), 0, $length) . '...';
}
}
$engine->addModifier('excerpt', new ExcerptModifier());
// {{ content | excerpt:200 }}
// Callable
$engine->addTag('icon', function(array $params): string {
$name = $params['name'] ?? '';
return "<svg class=\"icon\"><use href=\"#icon-{$name}\"></use></svg>";
});
// {{ icon name="star" }}
// Class with methods (namespace)
use Bugo\Antlers\Tags\AbstractTag;
class CacheTag extends AbstractTag
{
public function index(): string|array|null
{
$key = $this->param('key', 'default');
// ... cache logic
return $this->content();
}
public function forget(): string|array|null
{
$key = $this->param('key', 'default');
// ... clear cache
return null;
}
}
$engine->addTag('cache', new CacheTag());
// {{ cache key="homepage" }}...{{ /cache }}
// {{ cache:forget key="homepage" }}
$engine->addTag('repeat', function(array $params, array $data, $processor, $method, $children): string {
$times = (int) ($params['times'] ?? 1);
$output = '';
for ($i = 0; $i < $times; $i++) {
$output .= $processor->reduce($children, array_merge($data, ['iteration' => $i + 1]));
}
return $output;
});
// {{ repeat times="3" }}{{ iteration }}. Hello!{{ /repeat }}
$engine->setGlobals([
'site_name' => 'My Blog',
'year' => date('Y'),
'user' => $currentUser,
]);
// Available in all templates without passing them to render()
echo $engine->render('© {{ year }} {{ site_name }}');
$engine->setStrictMode(true);
echo $engine->render('{{ name }}', ['name' => 'Alice']);
// Alice
echo $engine->render('{{ missing }}');
// throws AntlersRuntimeException
bash
composer