2023-01-09 20:50:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace AntCMS;
|
|
|
|
|
|
|
|
use AntCMS\AntConfig;
|
|
|
|
|
|
|
|
class AntTwig
|
|
|
|
{
|
2023-03-07 10:09:32 +00:00
|
|
|
protected $twigEnvironment;
|
|
|
|
protected $theme;
|
|
|
|
|
|
|
|
public function __construct(string $theme = null)
|
2023-01-09 20:50:11 +00:00
|
|
|
{
|
2023-05-27 06:46:42 +00:00
|
|
|
$twigCache = (AntConfig::currentConfig('enableCache') !== 'none') ? AntCachePath : false;
|
2023-03-07 10:09:32 +00:00
|
|
|
$this->theme = $theme ?? AntConfig::currentConfig('activeTheme');
|
2023-01-09 20:50:11 +00:00
|
|
|
|
2023-03-07 10:09:32 +00:00
|
|
|
if (!is_dir(antThemePath . '/' . $this->theme)) {
|
|
|
|
$this->theme = 'Default';
|
2023-01-09 20:50:11 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 10:09:32 +00:00
|
|
|
$this->twigEnvironment = new \Twig\Environment(new \Shapecode\Twig\Loader\StringLoader(), [
|
2023-01-09 20:50:11 +00:00
|
|
|
'cache' => $twigCache,
|
2023-01-11 01:24:30 +00:00
|
|
|
'debug' => AntConfig::currentConfig('debug'),
|
2023-01-09 20:50:11 +00:00
|
|
|
]);
|
|
|
|
|
2023-03-07 10:09:32 +00:00
|
|
|
$this->twigEnvironment->addExtension(new \AntCMS\AntTwigFilters);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function renderWithSubLayout(string $layout, array $params = array())
|
|
|
|
{
|
|
|
|
$subLayout = AntCMS::getThemeTemplate($layout, $this->theme);
|
|
|
|
$mainLayout = AntCMS::getPageLayout($this->theme);
|
2023-03-31 00:02:12 +00:00
|
|
|
$siteInfo = AntCMS::getSiteInfo();
|
|
|
|
|
|
|
|
$params['AntCMSSiteTitle'] = $siteInfo['siteTitle'];
|
2023-03-07 10:09:32 +00:00
|
|
|
$params['AntCMSBody'] = $this->twigEnvironment->render($subLayout, $params);
|
|
|
|
|
|
|
|
return $this->twigEnvironment->render($mainLayout, $params);
|
|
|
|
}
|
2023-02-13 01:02:07 +00:00
|
|
|
|
2023-03-07 10:09:32 +00:00
|
|
|
public function renderWithTiwg(string $content = '', array $params = array())
|
|
|
|
{
|
2023-03-31 00:02:12 +00:00
|
|
|
$siteInfo = AntCMS::getSiteInfo();
|
|
|
|
$params['AntCMSSiteTitle'] = $siteInfo['siteTitle'];
|
|
|
|
|
2023-03-07 10:09:32 +00:00
|
|
|
return $this->twigEnvironment->render($content, $params);
|
2023-01-09 20:50:11 +00:00
|
|
|
}
|
|
|
|
}
|