twig - Symfony form theme avoid block output and use custom variables in block -
a. want render form , use form theme. block created outputted directly before doctype hidden input field created. want render in form(form) function.
b. can't use {{ template }} variable inside block or other variables created outside block? template variable created controller.
{# form theme #} {% form_theme form _self %} {% block _my_form_example__token_widget %} {% set type = type|default('hidden') %} <input data-test="is-this-render" type="{{ type }}" {{ block('widget_attributes') }} value="{{ render_esi( controller( 'mycontroller:form:token', { 'form': template } {# template variable can't accessed here?? #} ) ) }}" /> {% endblock %} <!doctype html> <html> <head> <title>basic form</title> </head> <body> <h1>basic form {{ template }}</h1>{# output works #} {{ form(form) }} </body> </html>
this output following:
<input data-is-rendered="test" type="hidden" value="...." /> <!-- should not here --> <!doctype html> <html> <head> <title>basic form</title> </head> <body> <h1>basic form template_variable_content</h1><!-- {{ template }} works here --> <form ....> <!-- ... ---> <input data-is-rendered="test" type="hidden" value="...." /> <!-- render correct when no template variable used --> <!-- ... ---> </form> </body> </html>
i think have misunderstood use of blocks in twig. when template uses extends
extend template, can use block
tag define areas replace named areas in parent template.
because you're template not extend template, use of block
tag tells twig child templates able replace portion of template defining block named "_my_form_example__token_widget".
in symfony documentation form theming, notice begin example important extends
tag.
{% extends 'base.html.twig' %} {% form_theme form _self %} {% block integer_widget %} <div class="integer_widget"> {% set type = type|default('number') %} {{ block('form_widget_simple') }} </div> {% endblock %} {% block content %} {# ... render form #} {{ form_row(form.age) }} {% endblock %}
even though base template example file extending has nothing form themes, fact child template switches block
tags defining areas replaced , makes them define areas can used replace. making template extend base should solve of described problems using block.
i suggest, though, use described method creating external file hold theme, though, can more keep forms consistent across pages , keep form theming stuff in central location you, , else might work on code, able find later.
Comments
Post a Comment