97e6a8a4e6
There's zero need to create a new instance of the class before calling it, so this removes a very small amount of overhead, but more importantly removes the need to create and define a new class of it each time, which makes the code very slightly cleaner
37 lines
1.1 KiB
PHP
37 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace AntCMS;
|
|
|
|
use AntCMS\AntConfig;
|
|
|
|
class AntTwig
|
|
{
|
|
/**
|
|
* @param string $content
|
|
* @param array<mixed> $params
|
|
* @param string|null $theme
|
|
* @return string
|
|
*/
|
|
public static function renderWithTiwg(string $content = '', array $params = array(), string $theme = null)
|
|
{
|
|
$twigCache = AntConfig::currentConfig('enableCache') ? AntCachePath : false;
|
|
$theme = $theme ?? AntConfig::currentConfig('activeTheme');
|
|
|
|
if (!is_dir(antThemePath . '/' . $theme)) {
|
|
$theme = 'Default';
|
|
}
|
|
|
|
$templatePath = AntTools::repairFilePath(antThemePath . '/' . $theme . '/' . 'Templates');
|
|
|
|
$filesystemLoader = new \Twig\Loader\FilesystemLoader($templatePath);
|
|
$stringLoader = new \Shapecode\Twig\Loader\StringLoader();
|
|
$chainLoader = new \Twig\Loader\ChainLoader([$stringLoader, $filesystemLoader]);
|
|
$twigEnvironment = new \Twig\Environment($chainLoader, [
|
|
'cache' => $twigCache,
|
|
'debug' => AntConfig::currentConfig('debug'),
|
|
]);
|
|
|
|
return $twigEnvironment->render($content, $params);
|
|
}
|
|
}
|