簡體   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