繁体   English   中英

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

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

我正在寻找一种无缝的方式来做到这一点。

使用 Symfony 3.4,我需要一种方法,让渲染页面知道,对于任何元素/文本,它来自哪个树枝模板。

注意:我已经检查了Twig - 如果启用了调试模式,则显示模板名称,但该线程是关于使用 Twig 而不是 Symfony。 我一直在关注那里的例子,但没有结果。

我不仅希望在每个模板中添加跟踪数据,而且还希望具有所需的最小侵入性代码,例如,添加到树枝文件的起点和文件末尾。

由于也有块,对于这些块中的每一个,相同但类似于<trace twig="…" block="…"/>等。

(当然这些标记中的样式=“显示:无”)

-------------- 尝试

我已将另一个引用的问题模板放在 templates/DebugTemplate.php、app/templates/debugTemplate.php 和 src/AppBundle/Twig/DebugTemplate.php

我去了 app/config/config.yml 并添加了 twig->base_template_class,但我不知道如何正确地使这个配置工作。

我明白你只需要直接写模板名称,如:

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

但它在第 116 行(“base_template_class: {'DebugTemplate'}”附近)因意外字符 (}) 而崩溃

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

通过控制台检查配置: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: ','

使用下一个配置示例(其他人不生成缓存文件):base_template_class:templates\\DebugTemplate

它在缓存中生成一个尝试扩展配置模板的类:

class __TwigTemplate_f3b8c130edb93e6c111c9714c114a4cede8e75482f6253fab3adbfd0ccca6c5e extends \templates\\DebugTemplate

当我更改配置时,这个类并不总是更新,所以我认为我在配置中输入的内容不被认为是有效的。

所以我有两个问题:

  • 在缓存中几乎找不到 DebugTemplate.php。
  • 我不知道如何在该配置字段中设置有效路径

如何在 Symfony 3.4(我不知道其他版本)中执行此操作,基于Twig - 如果启用调试模式和@DarkBee 帮助显示模板名称

脚步:

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

    \n 命名空间 YourCompany\\Application\\TWIG\\Helper;\n 使用 Twig_Template;\n\n 抽象类 DebugTemplate 扩展了 Twig_Template {\n     公共函数显示(数组 $context,数组 $blocks = array())\n      {\n          // 解决方法 - 仅在使用 @ 加载部分时添加 html 注释\n          if(substr($this->getTemplateName(),0,1) == '@') {\n              echo 'getTemplateName() 。  '-->';\n          }\n\n          $this->displayWithErrorHandling($this->env->mergeGlobals($context), array_merge($this->blocks, $blocks));\n\n          if(substr($this->getTemplateName(),0,1) == '@') {\n              echo 'getTemplateName() 。  '-->';\n          }\n\n      }\n  }\n 
  • 转到 config.yml。 在我的项目中,它的路径是 app/config/config.yml。 使用我们新的 namespace\\className 作为值,在 twig 部分下添加 base_template_class 设置。 例子:

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

暂无
暂无

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

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