简体   繁体   English

在渲染期间/之后向 Symfony 3.4 twig 模板添加调试跟踪

[英]Adding debug traces to Symfony 3.4 twig templates during/after render

I am looking for a seamless way to do this.我正在寻找一种无缝的方式来做到这一点。

With Symfony 3.4, I need a way to, having a rendered page, know, for any element/text, which twig template it comes from.使用 Symfony 3.4,我需要一种方法,让渲染页面知道,对于任何元素/文本,它来自哪个树枝模板。

NOTE: I have checked Twig - display template names if debug mode is enabled , but that thread is about using Twig but no Symfony.注意:我已经检查了Twig - 如果启用了调试模式,则显示模板名称,但该线程是关于使用 Twig 而不是 Symfony。 I have been following the examples there but without results.我一直在关注那里的例子,但没有结果。

I would like to not just add tracing data in every template, but to have the minimum possible invasive code needed to, for example, add to the starting point of a twig file and at the end of the file.我不仅希望在每个模板中添加跟踪数据,而且还希望具有所需的最小侵入性代码,例如,添加到树枝文件的起点和文件末尾。

As there are blocks as well, the same but something like <trace twig="…" block="…"/> , etc, to each one of those blocks.由于也有块,对于这些块中的每一个,相同但类似于<trace twig="…" block="…"/>等。

(Of course style=“display:none” in those marks) (当然这些标记中的样式=“显示:无”)

-------------- attempts -------------- 尝试

I have put the other quoted question template at templates/DebugTemplate.php, app/templates/debugTemplate.php, and src/AppBundle/Twig/DebugTemplate.php我已将另一个引用的问题模板放在 templates/DebugTemplate.php、app/templates/debugTemplate.php 和 src/AppBundle/Twig/DebugTemplate.php

I went to app/config/config.yml and added twig->base_template_class, but I do not know how to correctly make this configuration work.我去了 app/config/config.yml 并添加了 twig->base_template_class,但我不知道如何正确地使这个配置工作。

I have understood that you just have to write the template name directly like:我明白你只需要直接写模板名称,如:

# Twig Configuration
twig:
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    base_template_class: DebugTemplate

But it crashes with Unexpected characters (}) at line 116 (near "base_template_class: {'DebugTemplate'}")但它在第 116 行(“base_template_class: {'DebugTemplate'}”附近)因意外字符 (}) 而崩溃

Same with

base_template_class: Twig\DebugTemplate
base_template_class: 'Twig\DebugTemplate'
base_template_class: {'Twig\DebugTemplate'}
base_template_class: 'DebugTemplate'
base_template_class: "%kernel.root_dir%/templates/DebugTemplate"
base_template_class: "%kernel.root_dir%\\templates\\DebugTemplate"  
base_template_class: templates\\DebugTemplate

Checking config via console: php bin/console debug:config twig通过控制台检查配置:php bin/console debug:config twig

Current configuration for extension with alias "twig"
=====================================================
twig:
    debug: true
    strict_variables: true
    base_template_class: DebugTemplate
    exception_controller: 'twig.controller.exception:showAction'
    form_themes:
        - form_div_layout.html.twig
    globals: {  }
    autoescape: name
    autoescape_service: null
    autoescape_service_method: null
    cache: /NFS/code/nabc/abc_frontal_service/var/cache/dev/twig
    charset: UTF-8
    default_path: /NFS/code/nabc/abc_frontal_service/templates
    paths: {  }
    date:
        format: 'F j, Y H:i'
        interval_format: '%d days'
        timezone: null
    number_format:
        decimals: 0
        decimal_point: .
        thousands_separator: ','

Using the next config example (others do not generate the cache file): base_template_class: templates\\DebugTemplate使用下一个配置示例(其他人不生成缓存文件):base_template_class:templates\\DebugTemplate

It is generated, in cache, a class that tryes to extend the configurated template:它在缓存中生成一个尝试扩展配置模板的类:

class __TwigTemplate_f3b8c130edb93e6c111c9714c114a4cede8e75482f6253fab3adbfd0ccca6c5e extends \templates\\DebugTemplate

This class is not always renewed when I change the config, so I suppose that what I typed in the config was not considered valid.当我更改配置时,这个类并不总是更新,所以我认为我在配置中输入的内容不被认为是有效的。

So I have two problems:所以我有两个问题:

  • the DebugTemplate.php is neither nowhere near to be found in the cache.在缓存中几乎找不到 DebugTemplate.php。
  • I do not know how to set a valid path in that config field我不知道如何在该配置字段中设置有效路径

How to do this in Symfony 3.4 (I do not know about other versions), based on Twig - display template names if debug mode is enabled and @DarkBee helping hands!如何在 Symfony 3.4(我不知道其他版本)中执行此操作,基于Twig - 如果启用调试模式和@DarkBee 帮助显示模板名称

Steps:脚步:

  • Create your template class.创建您的模板类。 Example: src/YourCompany/Application/TWIG/Helper/DebugTemplate.php示例:src/YourCompany/Application/TWIG/Helper/DebugTemplate.php

    \n namespace YourCompany\\Application\\TWIG\\Helper;命名空间 YourCompany\\Application\\TWIG\\Helper;\n use Twig_Template;使用 Twig_Template;\n\n abstract class DebugTemplate extends Twig_Template {抽象类 DebugTemplate 扩展了 Twig_Template {\n     public function display(array $context, array $blocks = array())公共函数显示(数组 $context,数组 $blocks = array())\n     { {\n         // workaround - only add the html comment when the partial is loaded with @ // 解决方法 - 仅在使用 @ 加载部分时添加 html 注释\n         if(substr($this->getTemplateName(),0,1) == '@') { if(substr($this->getTemplateName(),0,1) == '@') {\n             echo 'getTemplateName() . echo 'getTemplateName() 。 ' -->'; '-->';\n         } }\n\n         $this->displayWithErrorHandling($this->env->mergeGlobals($context), array_merge($this->blocks, $blocks)); $this->displayWithErrorHandling($this->env->mergeGlobals($context), array_merge($this->blocks, $blocks));\n\n         if(substr($this->getTemplateName(),0,1) == '@') { if(substr($this->getTemplateName(),0,1) == '@') {\n             echo 'getTemplateName() . echo 'getTemplateName() 。 ' -->'; '-->';\n         } }\n\n     } }\n } }\n 
  • go to config.yml.转到 config.yml。 In my project its path is app/config/config.yml.在我的项目中,它的路径是 app/config/config.yml。 Add the base_template_class setting under the twig section using our new namespace\\className as the value.使用我们新的 namespace\\className 作为值,在 twig 部分下添加 base_template_class 设置。 Example:例子:

    \n ... ...\n\n # Twig Configuration # 树枝配置\n twig:枝条:\n     debug: '%kernel.debug%'调试: '%kernel.debug%'\n     strict_variables: '%kernel.debug%' strict_variables: '%kernel.debug%'\n     base_template_class: YourCompany\\Application\\TWIG\\Helper\\DebugTemplate base_template_class: YourCompany\\Application\\TWIG\\Helper\\DebugTemplate\n\n 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM