Custom Twig Form Field Template

I would like to create my own template in the branch to display the form field.

Example:

{{ form_row(form.field) }}

This can be undone by creating a form.

{% block form_row %}
... custom code
{% endblock form_row %}

What I would like to do is:

{% block custom_row %}
... custom code
{% endblock custom_row %}

and use it as follows:

{{ custom_row(form.field }}

however, this raises the exception that the method was custom_rownot found.

I understand that this can be done with the Twig extension, but I do not know how to register a block as a function.

Update

what i really want:

I use twitter bootstrap and a package that overrides all form themes. And that makes the div around the radio, so it cannot be embedded. So I wanted to do something like this:

copy your template and get rid of the div:

{% block inline_radio_row %}
    {% spaceless %}
        {% set col_size = col_size|default(bootstrap_get_col_size()) %}

        {% if attr.label_col is defined and attr.label_col is not empty %}
            {% set label_col = attr.label_col %}
        {% endif %}
        {% if attr.widget_col is defined and attr.widget_col is not empty %}
            {% set widget_col = attr.widget_col %}
        {% endif %}
        {% if attr.col_size is defined and attr.col_size is not empty %}
            {% set col_size = attr.col_size %}
        {% endif %}

        {% if label is not sameas(false) %}
            {% if not compound %}
                {% set label_attr = label_attr|merge({'for': id}) %}
            {% endif %}
            {% if required %}
                {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
            {% endif %}
            {% if label is empty %}
                {% set label = name|humanize %}
            {% endif %}
            {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' radio-inline')|trim}) %}
            <label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
                {{ block('radio_widget') }}
                {{ label|trans({}, translation_domain) }}
            </label>
        {% else %}
            {{ block('radio_widget') }}
        {% endif %}
        {{ form_errors(form) }}
    {% endspaceless %}
{% endblock inline_radio_row %}

and then

{{ inline_radio_row(form.field) }}

ifs div, (radio-inline). , . , - .

2

:

class FormExtension extends \Twig_Extension
{
    public function getFunctions()
    {
        return array(
            'inline_radio_row'  => new \Twig_Function_Node(
                'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode',
                array('is_safe' => array('html'))
            ),
        );
    }
}

, , , . - , ?

3

http://twig.sensiolabs.org/doc/tags/include.html

+4
2

twig :

:

<div class="form_row">
    {{ form_label(form.field) }} {# the name of the field #}
    {{ form_errors(form.field) }} {# the field #}
    {{ form_widget(form.field) }} {# the errors associated to the field #}
</div>
+1

.

:

1.

public function getName() {
  return 'hrQuestionResponse';
}

2.

{% form_theme form 'InterlatedCamsBundle:Form:fields.html.twig' %}

3.

. bootstrap, , , , . /vendor/braincrafted/bootstrap -bundle/Braincrafted/Bundle/BootstrapBundle/Resources/views/Form/bootstrap.html.twig, radio_row. , , . 2.7 .

4.

, FormType, 1.

.

.

{% block hrQuestionResponse_widget %}
hrQuestionResponse_row
{% spaceless %}
    {% set class = '' %}
...
 {% endspaceless %}
{% endblock hrQuestionResponse_widget %}

, form_widget(), _widget. , , radio_row.

0

All Articles