문제

I am trying to alter the date_widget using symfony2 and twig. I would like to display just the year.

After reading Symfony 2 date input with only year selector and the symfony2 book chapter on templating forms I have copied the date_widget block into my template and am receiving the error:

Variable "widget" does not exist in bundle....

here is the code from my template:

{% extends '::base.html.twig' %}
{% block body %}

{% block date_widget %}
{% spaceless %}
    {% if widget == 'single_text' %}
         {{ block('field_widget') }}
    {% else %}
        <div {{ block('widget_container_attributes') }}>
            {{ date_pattern|replace({
                '{{ year }}': form_widget(form.year),
            })|raw }}
        </div>
    {% endif %}
{% endspaceless %}
{% endblock date_widget %}

....print form (generated though app/console generate:crud)

{% endblock %}

Any Thoughts? Thanks!

도움이 되었습니까?

해결책

put the date_widget block outside of the body block, and add the following code after the initial extends

{% form_theme form _self %}

now, your code should look like this

{% extends '::base.html.twig' %}
{% form_theme form _self %}

{% block body %}

....print form (generated though app/console generate:crud)

{% endblock %}

{% block date_widget %}
{% spaceless %}
    {% if widget == 'single_text' %}
         {{ block('field_widget') }}
    {% else %}
        <div {{ block('widget_container_attributes') }}>
            {{ date_pattern|replace({
                '{{ year }}': form_widget(form.year),
            })|raw }}
        </div>
    {% endif %}
{% endspaceless %}
{% endblock date_widget %}

http://symfony.com/doc/current/book/forms.html#form-theming

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top