let\'s say I have created a custom twig function: templateName.
$twig = new Twig_Environment($loader);
$twig->addFunction(\'templateName\', new Twig_Func
The solution proposed by Mario A did not work for me, but using debug_backtrace() is a great idea and a little modification made it work with a recent Twig version:
private function getTwigTemplateName()
{
foreach (debug_backtrace() as $trace) {
if (isset($trace['object'])
&& (strpos($trace['class'], 'TwigTemplate') !== false)
&& 'Twig_Template' !== get_class($trace['object'])
) {
return $trace['object']->getTemplateName() . "({$trace['line']})";
}
}
return '';
}