class Project_Twig_Extension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('test', [$this, 'getTest'], ['is_safe'=>['html']]),
];
}
public function getTest() {
return '<div>test</div>';
}
}
$twig = new \Twig\Environment($loader);
$twig->addExtension(new Project_Twig_Extension());
如果突然有人需要它。创建一个类
一切都很好。关键是显示
html
,参数负责这个['is_safe'=>['html']]
这是一篇非常酷的文章https://twig.symfony.com/doc/3.x/advanced.html#creating-an-extension。一切都详细描述。