0

We want to change the way we do the frontends in symfony and we'd like some level of "reflection":

We want the system to be able to detect "Template displayProduct.html.twig needs xxxx other file, defines yyyy block and uses the zzzz variable".

I would like a command similar to:

php bin/console debug:template displayProduct.html.twig

that responds something like this:

Template: displayProduct.html.twig

Requires: # And tells us what other files are needed
    - widgetPrice.html.twig
    - widgetAvailability.html.twig

Defines: # And tells us what {% block xxxx %} are defined
    - body
    - title
    - javascripts_own
    - javascripts_general

Uses these variables: # <= This is the most important for us now
    - productTitle
    - price
    - stock
    - language

We are now visually scanning complex templates for needed variables and it's a killer. We need to automatically tell "this template needs this and that to work".

PD: Functional tests are not the solution, as we want to apply all this to dynamically generated templates stored in databases, to make users able to modify their pages, so we can't write a test for every potential future unknown template that users will write.

Does this already exist somehow?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Xavi Montero
  • 9,239
  • 7
  • 57
  • 79
  • 1
    I don't think it does or that it's feasible at all, but I could be wrong. Some elements (like blocks or macros) may require some argument used conditionally. If there's no runtime variables passed it's impossible to evaluate the output. – msg Sep 06 '22 at 14:52
  • I don't know of any such tool. I do know that `suggest a tool` type question are often not a good fit for stackoverflow. Depends on how frisky the closing posse feels. Might consider posting on the [Symfony Discussion](https://github.com/symfony/symfony/discussions) board and/or the [Symfony Reddit](https://www.reddit.com/r/symfony/new/) board. – Cerad Sep 06 '22 at 14:59
  • @Cerad Just in case... I was not suggesting any tool, nor asking for a "tool". Just asking how to debug, as when we debug in PhpUnit or via printing echoes. Asking if the 'symfony" itself has classes and commands to consume to achieve this. – Xavi Montero Sep 06 '22 at 15:09
  • Reflecting on this, `Environment`, `Template` and `Parser` [classes](https://github.com/twigphp/Twig/tree/3.x/src) could give you some of the basic information that you are looking for: at the very least block names and included templates. I still think that variables will be next to impossible because it would need evaluation but at least is a start. Looking at the api should be pretty straightforward what methods will be useful, but if you need some further information give me a mention. Good luck. – msg Sep 07 '22 at 14:55
  • @XaviMontero Looks like this (newer) [question](https://stackoverflow.com/questions/73647517/get-the-raw-filter-when-parsing-twig-template) can give you a direction on how to do this – DarkBee Sep 08 '22 at 14:15

0 Answers0