Как установить twig?
composer require twig/twig
Как подключить twig?
$loader = new Twig_Loader_Filesystem('./templates');
$twig = new Twig_Environment($loader, array(
'cache' => './cache',
// 'debug' => true,
));
$template = $twig->load('index.html.twig');
echo $template->render();
Как добавить глобальную переменную?
$twg->addGlobal('siteName', 'Тест');
Как добавить кастомные функции, проверки, фильтры?
/**
* Class MyTwigExtension
*/
class MyTwigExtension extends \Twig_Extension
{
/**
* @return array
*/
public function getTests()
{
return array(
new \Twig_SimpleTest('instance_of', array($this, 'isInstanceOfTest'))
);
}
/**
* @return array
*/
public function getFunctions()
{
return array(