I'm trying to render a form on a twig template if its is defined in the render call on controller. Something like:
{% if form_to_be_rendered is defined %}
{{ form(form_to_be_rendered) }}
{% endif }
If the controller's render call includes the var of form_to_be_rendered
, everything runs well, and the form is rendered. But if I try to render the template without this argument, Twig throws a RuntimeError
indicating that the form_to_be_rendered
variable is not defined.
Variable "form_to_be_rendered" does not exist in BundleName:Path/to/template:template.html.twig at line 3
500 Internal Server Error - Twig_Error_Runtime
I've tried passing it as a null value and is not null
check on condition, but it fails too.
I put this dump on template:
{% dump reset_password_form is defined %}
And it is evaluated as false
when I don't pass any arguments to render function.
EDIT
I forgot to post that there is a {% block content %}
inside the conditional block which causes the issue. Please view the solution below.
Thanks,
if <var> is defined
guard – mzulchform
function) should use expression delimiters ({{ }}
) rather than tag delimiters ({% %}
) though I would expectTwig_Error_Syntax
instead ofTwig_Error_Runtime
if that were the primary issue – mzulchif
orform()
? – Kaivosukeltaja