Twig - display template names if debug mode is enabled

前端 未结 3 605
一个人的身影
一个人的身影 2021-01-07 01:33

I\'m using Twig lately and I was wondering if it is possible to output the template names which are loaded on the page. The best way I can think of is to display the name ab

3条回答
  •  盖世英雄少女心
    2021-01-07 02:14

    Thanks to @DarkBee I was pointed to the right direction and ended up using this: I created a debug-template.class.php with the following content:

    getTemplateName(),0,1) == '@') {
                echo '';
            }
    
            $this->displayWithErrorHandling($this->env->mergeGlobals($context), array_merge($this->blocks, $blocks));
    
            if(substr($this->getTemplateName(),0,1) == '@') {
                echo '';
            }
    
        }
    }
    ?>
    

    Then I took my index.php and added

    require_once 'vendor/twig/twig/lib/Twig/TemplateInterface.php';
    require_once 'vendor/twig/twig/lib/Twig/Template.php';
    

    and added the DebugTemplate class

    $twig = new Twig_Environment($loader, array(
        'cache' => false,
        'base_template_class' => 'DebugTemplate'
    ));
    

    The result is just what I want and looks like this

    
            

提交回复
热议问题