misuzu/src/tpl.php

69 lines
1.6 KiB
PHP
Raw Normal View History

2018-08-15 03:12:58 +02:00
<?php
use Misuzu\Twig;
use Misuzu\TwigMisuzu;
2018-08-15 03:12:58 +02:00
define('MSZ_TPL_FILE_EXT', '.twig');
define('MSZ_TPL_VARS_STORE', '_msz_tpl_vars');
function tpl_init(array $options = []): void
{
$options = array_merge([
'cache' => false,
'strict_variables' => true,
'auto_reload' => false,
'debug' => false,
], $options);
$GLOBALS[MSZ_TPL_VARS_STORE] = [];
$loader = new Twig_Loader_Filesystem;
$twig = new Twig($loader, $options);
$twig->addExtension(new Twig_Extensions_Extension_Date);
$twig->addExtension(new TwigMisuzu);
2018-08-15 03:12:58 +02:00
}
function tpl_var(string $key, $value): void
{
$GLOBALS[MSZ_TPL_VARS_STORE][$key] = $value;
}
function tpl_vars(array $vars): void
{
$GLOBALS[MSZ_TPL_VARS_STORE] = array_merge($GLOBALS[MSZ_TPL_VARS_STORE], $vars);
}
function tpl_add_path(string $path): void
{
Twig::instance()->getLoader()->addPath($path);
}
function tpl_sanitise_path(string $path): string
{
// if the .twig extension if already present just assume that the path is already correct
if (ends_with($path, MSZ_TPL_FILE_EXT)) {
return $path;
}
return str_replace('.', '/', $path) . MSZ_TPL_FILE_EXT;
}
function tpl_exists(string $path): bool
{
return Twig::instance()->getLoader()->exists(tpl_sanitise_path($path));
}
function tpl_render(string $path, array $vars = []): string
{
if (!defined('MSZ_TPL_RENDER')) {
define('MSZ_TPL_RENDER', microtime(true));
}
2018-08-15 03:12:58 +02:00
$path = tpl_sanitise_path($path);
if (count($vars)) {
tpl_vars($vars);
}
2018-09-16 21:51:14 +02:00
return Twig::instance()->render($path, $GLOBALS[MSZ_TPL_VARS_STORE]);
2018-08-15 03:12:58 +02:00
}