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.
NOTE: I have checked Twig - display template names if debug mode is enabled, but that thread is about using Twig but no 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.
(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
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.
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'}")
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
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
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.
- I do not know how to set a valid path in that config field