在树枝扩展中获取模板名称

Getting template name in twig extension

是否可以获取在 twig 扩展中调用方法的模板的名称?

示例:

private $templateInUse;

public function __construct(){
    $templateInUse = $this->someMagicalFunction();
}

/**
 * {@inheritdoc}
 */
public function getName() {
    return 'twig_extension';
}

public function getTokenParsers()
{
    return array(new TokenParser($this->templateInUse));
}

它最终会输出 some:file:path.html.twig

事实证明,您可以从流本身获取模板名称。

class FooTokenParser extends Twig_TokenParser
{

    public function parse(Twig_Token $token)
    {

        $this->parser->getStream()->getFilename();
    }
}