Symfony 2 - Get the CSRF-Token of a form in a Twig template

You probably already know that you can get the value of each widget of a form using

form.vars.value.{widget_name}  

in your Twig template (where {widget_name} is the name of the widget you are trying to get the value of). However, when trying to get the CSRF-Token this way, you will get an error that the variable _token does not exist. To access the CSRF-Token of a Symfony 2 form in Twig you have to use

form._token.vars.value  

This way you can use it in JavaScripts for example (I actually used it to dynamically build post-delete-buttons).

comments powered by Disqus