how to check all available variable that have been passed form controller to twig template

帅比萌擦擦* 提交于 2019-12-13 21:47:04

问题


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


来源:https://stackoverflow.com/questions/45528452/how-to-check-all-available-variable-that-have-been-passed-form-controller-to-twi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!