0

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

kristyan
  • 81
  • 10

3 Answers3

2

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

kristyan
  • 81
  • 10
  • This is very useful information, and the first time I found out about it. Make sure you don't use `dump` in your `prod` environment though. You'll get errors. – Alvin Bunk Aug 06 '17 at 06:04
0

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) }}
Imanali Mamadiev
  • 2,604
  • 2
  • 15
  • 23
  • yes that is alternative syntax for passing variable but my point of question is how to check all variable that been passed, if a have a lot variable from controller to twig so I wont miss any of them – kristyan Aug 06 '17 at 04:28
0

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
DarkBee
  • 16,592
  • 6
  • 46
  • 58