How to var_dump variables in twig templates?

后端 未结 14 1470
滥情空心
滥情空心 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:21

    The complete recipe here for quicker reference (note that all the steps are mandatory):

    1) when instantiating Twig, pass the debug option

    $twig = new Twig_Environment(
    $loader, ['debug'=>true, 'cache'=>false, /*other options */]
    );
    

    2) add the debug extension

    $twig->addExtension(new \Twig_Extension_Debug());
    

    3) Use it like @Hazarapet Tunanyan pointed out

    {{ dump(MyVar) }}
    

    or

    {{ dump() }}
    

    or

    {{ dump(MyObject.MyPropertyName) }}
    

提交回复
热议问题