PHP code example of comhon-project / template-renderer
1. Go to this page and download the library: Download comhon-project/template-renderer 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/ */
comhon-project / template-renderer example snippets
use Comhon\TemplateRenderer\Locale\LocaleHandler;
class MyLocaleHandler implements LocaleHandler
{
public function getLocale(): ?string { /* return your application locale */ }
public function setLocale(?string $locale): void { /* change your application locale */ }
}
$templateManager = new TemplateManager($config, new MyLocaleHandler);
use Comhon\TemplateRenderer\RenderOptions;
Template::render($template, $replacements, new RenderOptions(locale: 'fr', timezone: 'Europe/Paris', preferredTimezone: 'Asia/Tokyo'));
class User
{
public function toRenderOptions(): RenderOptions
{
return new RenderOptions(locale: $this->locale, preferredTimezone: $this->timezone);
}
}
Template::render($template, $replacements, $user->toRenderOptions());
use Comhon\TemplateRenderer\RenderContext;
$templateManager->render($template, fn (RenderContext $context) => [
// full date in the render timezone and locale (e.g. "samedi 12 décembre 2026 à 15:00")
'ordered_at' => IntlDateFormatter::formatObject(
$order->created_at->setTimezone(new DateTimeZone($context->timezone())),
[IntlDateFormatter::FULL, IntlDateFormatter::SHORT],
$context->locale(),
),
]);
Template::render(
"{{ meeting|date('H:i') }} / {{ meeting|date('H:i', 'Asia/Shanghai') }} / {{ birthday|date('d/m', 'America/New_York') }}",
fn (RenderContext $context) => [
'meeting' => $context->dateTime('2026-12-12 14:00'),
'birthday' => $context->date('2026-12-12'),
],
new RenderOptions(timezone: 'Europe/Paris'),
);
// 14:00 / 21:00 / 12/12 (a regular date at midnight in Paris would give 11/12 in New York)
use Comhon\TemplateRenderer\Drivers\DriverInterface;
use Comhon\TemplateRenderer\RenderContext;
class MyDriver implements DriverInterface
{
public function __construct(private array $config) {}
public function render(string $template, array $replacements, RenderContext $context): string
{
// render with $context->locale(), $context->timezone() and $context->preferredTimezone()
}
public function validate(string $template) {}
}
use Comhon\TemplateRenderer\RenderOptions;
class MyRenderOptions extends RenderOptions
{
public function __construct(
?string $locale = null,
?string $timezone = null,
?string $preferredTimezone = null,
public readonly ?string $currency = null,
) {
parent::__construct($locale, $timezone, $preferredTimezone);
}
}
Template::render(
'Total: {{ total }}',
fn (RenderContext $context) => [
'total' => (new NumberFormatter($context->locale(), NumberFormatter::CURRENCY))
->formatCurrency($order->total, $context->options()->currency),
],
new MyRenderOptions(locale: 'fr', currency: 'EUR'),
);
// Total: 12,50 €
// in MyDriver::render()
$options = $context->options();
if ($options instanceof MyRenderOptions) {
// use $options->currency
}
Template::extend('my-driver', fn (array $config) => new MyDriver($config));
'renderers' => [
'my-renderer' => [
'driver' => 'my-driver',
// options given to your driver
],
],
use Comhon\TemplateRenderer\Renderer;
$renderer = new Renderer(new MyDriver($config), new MyLocaleHandler); // the locale handler is optional
$renderer->render($template, fn (RenderContext $context) => [
'meeting' => $context->dateTime('2026-12-12 14:00'),
], new RenderOptions(locale: 'fr', timezone: 'Europe/Paris'));