How to var_dump variables in twig templates?

后端 未结 14 1474
滥情空心
滥情空心 2020-12-22 17:43

View layer pattern where you only present what you have been given is fine and all, but how do you know what is available? Is there a \"list all defined variables\" function

14条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-22 18:27

    As most good PHP programmers like to use XDebug to actually step through running code and watch variables change in real-time, using dump() feels like a step back to the bad old days.

    That's why I made a Twig Debug extension and put it on Github.

    https://github.com/delboy1978uk/twig-debug

    composer require delboy1978uk/twig-debug

    Then add the extension. If you aren't using Symfony, like this:

    addExtension(new DebugExtension());
    

    If you are, like this in your services YAML config:

    twig_debugger:
        class: Del\Twig\DebugExtension
        tags:
            - { name: twig.extension }
    

    Once registered, you can now do this anywhere in a twig template:

    {{ breakpoint() }}
    

    Now, you can use XDebug, execution will pause, and you can see all the properties of both the Context and the Environment.

    Have fun! :-D

提交回复
热议问题