假设我创建了一个自定义树枝函数:templateName。
$twig = new Twig_Environment($loader);
$twig->addFunction('templateName', new Twig_Function_Function('twig_template_name', array('needs_environment' => true)));
Run Code Online (Sandbox Code Playgroud)
有没有办法在 php.ini 中获取当前模板的名称?我想象这样的事情:
function twig_template_name(Twig_Environment $env, $values = null) {
return $env->getCurrentTemplateName();
}
Run Code Online (Sandbox Code Playgroud)
提前致谢。
对于每个需要回答最初问题的人,我找到了 twig 本身在 Twig_Error 类中使用的解决方案。
protected function guessTemplateInfo()
{
$template = null;
foreach (debug_backtrace() as $trace) {
if (isset($trace['object']) && $trace['object'] instanceof Twig_Template && 'Twig_Template' !== get_class($trace['object'])) {
$template = $trace['object'];
}
}
// update template filename
if (null !== $template && null === $this->filename) {
$this->filename = $template->getTemplateName();
}
/* ... */
Run Code Online (Sandbox Code Playgroud)
此致!