-->

how to check all available variable that have been

2019-09-17 16:32发布

问题:

how to check all variable exist

Exp in Controller return this to pass to twig template :

return $this->render('index/index.html.twig', [ 'department'=>$departments,'URILink'=>$URILink,'departmentDetail'=>$departmentDetails, 'contentCell'=>$this->mContentCell ]);

then twig template can reuse those variable by doin this

index.html.twig:

</div> {{ include ('department_list.html.twig',{'departments':department,'URILink':URILink}) }}</div>

for Comparison in smarty template we can use this:

get_template_vars() — returns assigned variable value(s)

how to do that with same analogy in twig template? in case i want to make sure all variable have been passed correctly

回答1:

after read this How to retrieve all Variables from a Twig Template?

i found out just simple

{%dump%}

will look all variables passed on a template



回答2:

You can include a template like this per http://symfony.com/doc/current/book/templating.html#including-other-templates

{{ include('YourBundle:ControllerName:yourAction.html.twig', {'variableName': yourData}) }}

Or like this per http://twig.sensiolabs.org/doc/tags/include.html

{% include 'template.html' with {'foo': 'bar'} %}

If you want to check variable use function in dev mod:

{{ dump(yourVariable) }}


回答3:

Instead of verifying that each variable exists when you pass it, it's better practice, imho, to utilize the filter default in this case

<div>
    {{ include ('department_list.html.twig',  'departments':department|default(null),'URILink':URILink|default(null) }}
</div