Symfony / Twig:表单小部件的内部ID?

I've to modify the radio_widget and would like to give the <label> and <input> the same ID, which should be unique for each pair.

Currently I'm using random(), but I'd prefer an internal counter, something like {{loop.index}} (Twig: for), to avoid conflicts.

{% block radio_widget -%}
    {% set id = random() %}
    <div class="radio">
        <label for="radio_{{ id }}">Label for {{ id }}</label>
        <input type="radio" id="radio_{{ id }}" {# ...#}/>
    </div>
{%- endblock radio_widget %}

Does anyone know a better solution?

Thanks in advance!

For every form-widget there already exists a pre-calculated unique id.

See: \Symfony\Component\Form\Extension\Core\Type\BaseType::buildView

In twig this ID is accessible through {{ form.vars.id }}.

In essence the id is just a concatenation of the nested form-type names.


Note that form.vars in general contains all the useful stuff you'd need to do form-customization. It's also the place to put custom values when using FormTypeInterface::buildView or FormTypeInterface::finishView to transport data to the form rendering.

ProjectTwigExtension.php

class ProjectTwigExtension extends Twig_Extension {

    public function getFunctions() {
        return array(
            new Twig_SimpleFunction('get_unique_key', array($this, 'getUniqueKey')),            
        );
    }

    private $keys = array();
    /**
    *   Create an unique HTML identifier for a form element
    *
    *   @param $name String to make unique
    *
    *   @returns String
    */
    public function getUniqueKey($name) {
        if (!in_array($name, $this->keys)) {
            $this->keys[] = $name;
            return $name;
        }
        $i = 0;
        while(in_array($name.++$i,$this->keys)) {}
        $this->keys[] = $name.$i;
        return $name.$i;
    }


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

config.php

$twig = new Twig_Environment($loader);
$twig->addExtension(new ProjectTwigExtension());

template.twig

{% block radio_widget -%}
    {% set id = get_unique_key('radio_') %}
    <div class="radio">
        <label for="{{ id }}">Label for {{ id }}</label>
        <input type="radio" id="{{ id }}" {# ...#}/>
    </div>
{%- endblock radio_widget %}

May be you can try something like this:

{% block radio_widget -%}
  {% if counter is defined %} {# set desired id #}
    {% set id = counter %}
  {% else %}
    {% set id = random() %} {# default value #}
  {% endif %}
    <div class="radio">
        <label for="radio_{{ id }}">Label for {{ id }}</label>
        <input type="radio" id="radio_{{ id }}" {# ...#}/>
    </div>
{%- endblock radio_widget %}

Example of use:

{% for i in 0..10 %}
  {% set counter = loop.index %}
  {{- block('radio_widget') }}
{% endfor %}