Reputation: 1161
I'm creating a custom form theme to generate the proper HTML for a custom field type. I registered my twigfile and it works fine, except for a strange function not found error.
I copied the parts from the default theme (form_div_layout) that create a collection type. I then changed the block names to avoid interference with default styling. Before changing the block names, it works and even uses my local definitions, but afterwards twig doesn't seem to be able to use a block as a function (although it works in the default theme). Could someone give me a hint?
This works, just copied from the default template.
{% block sortableCollection_widget %}
{% spaceless %}
{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': form_row(prototype) }) %}
{% endif %}
<div {{ block('widget_container_attributes') }}>
{% if form.parent is empty %}
{{ form_errors(form) }}
{% endif %}
{{ block('sortableCollection_rows') }}
{{ form_rest(form) }}
</div>
{% endspaceless %}
{% endblock %}
{% block sortableCollection_rows %}
{% spaceless %}
<ol class='.collection-editor .collection-sortable'>
{% for child in form %}
{{ form_row(child) }}
{% endfor %}
</ol>
{% endspaceless %}
{% endblock sortableCollection_rows %}
{% block form_row %}
{% spaceless %}
<li>using my template
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</li>
{% endspaceless %}
{% endblock form_row %}
{% block form_rows %}
{% spaceless %}
{% for child in form %}
{{ form_row(child) }}
{% endfor %}
{% endspaceless %}
{% endblock form_rows %}
Now I rename the form_row to sortableCollection_row and I get this error: The function "sortableCollection_row" does not exist in DTAMetadataBundle:Form:sortableCollection.html.twig at line 39
And it points to the place where sortableCollection_row(child) is called.
{% block sortableCollection_widget %}
{% spaceless %}
{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': form_row(prototype) }) %}
{% endif %}
<div {{ block('widget_container_attributes') }}>
{% if form.parent is empty %}
{{ form_errors(form) }}
{% endif %}
{{ block('sortableCollection_rows') }}
{{ form_rest(form) }}
</div>
{% endspaceless %}
{% endblock %}
{% block sortableCollection_rows %}
{% spaceless %}
<ol class='.collection-editor .collection-sortable'>
{% for child in form %}
{{ form_row(child) }}
{% endfor %}
</ol>
{% endspaceless %}
{% endblock sortableCollection_rows %}
{% block sortableCollection_row %}
{% spaceless %}
<li>using my template
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
</li>
{% endspaceless %}
{% endblock sortableCollection_row %}
{% block form_rows %}
{% spaceless %}
{% for child in form %}
{{ sortableCollection_row(child) }} // ERROR.
{% endfor %}
{% endspaceless %}
{% endblock form_rows %}
An idea, someone? Thanks!
Upvotes: 1
Views: 190
Reputation: 2893
form_row
is a twig function and also a twig block. Thus when you call form_row(...)
you're actually calling a twig function and not actually calling the block directly. The function will include the block content so I can see how it can be confusing.
So trying to call sortableCollection_row
is not valid because its not actually a function, its a block. Get it? You can potentially do something like this:
{% block form_rows %}
{% spaceless %}
{% for child in form %}
{{ block('sortableCollection_row') }} // NO ERROR
{% endfor %}
{% endspaceless %}
{% endblock form_rows %}
Now inside your sortableCollection_row
block instead of referencing form
you'll want to reference child
since you're no longer passing the child variable to the block.
Upvotes: 2