Is there a complete list of variables that can be

2019-03-09 13:55发布

问题:

I want to get all the variables available in the Symfony form theme file form_div_layout.html.twig, I read the Symfony official documention and searched on the web, but couldn't find any useful information on this, can someone help me?

回答1:

Well, you can get all the available variables in each block by iterating the context:

{% block form_widget_simple %}
<ol>
    {% for key, value in _context %}
    <li>{{ key }}</li>
    {% endfor %}
</ol>
{% spaceless %}
    {% set type = type|default('text') %}
    <input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
{% endspaceless %}
{% endblock form_widget_simple %}

And if you want to use yours, then you'll have to overwrite the classes that actually are rendering those widgets, just take a look at AbtractType::buildView...

As @Gregoire suggested, you can use {{ dump(_context) }} from version 1.5 (http://twig.sensiolabs.org/doc/functions/dump.html), but be aware that it will print a big amount of info.



回答2:

I hit the same problem recently, being the lack of documentation on the available variables (attributes) when working in themes. In the end I found my solution by searching through the vendor folder (took a while) for the variables I did know, to see what else is available.

The best place for me was to look in here: Symfony\Component\Form\Extension\Core\Type

The base type, being FieldType provides these variables via buildView

    $view
        ->set('form', $view)
        ->set('id', $id)
        ->set('name', $name)
        ->set('full_name', $fullName)
        ->set('errors', $form->getErrors())
        ->set('value', $form->getClientData())
        ->set('read_only', $form->isReadOnly())
        ->set('required', $form->isRequired())
        ->set('max_length', $form->getAttribute('max_length'))
        ->set('pattern', $form->getAttribute('pattern'))
        ->set('size', null)
        ->set('label', $form->getAttribute('label'))
        ->set('multipart', false)
        ->set('attr', $form->getAttribute('attr'))
        ->set('types', $types)
    ;

prototype is an attribute that only exists in the collection type, as is allow_add and allow_delete, see CollectionType in the same folder.

After the base FieldType, this appears to be the complete list.

CheckboxType.php:        ->setAttribute('value', $options['value'])
ChoiceType.php:          ->setAttribute('choice_list', $options['choice_list'])
ChoiceType.php:          ->setAttribute('preferred_choices', $options['preferred_choices'])
ChoiceType.php:          ->setAttribute('multiple', $options['multiple'])
ChoiceType.php:          ->setAttribute('expanded', $options['expanded'])
ChoiceType.php:          ->setAttribute('required', $options['required'])
ChoiceType.php:          ->setAttribute('empty_value', $emptyValue)
CollectionType.php:      ->setAttribute('prototype', $prototype->getForm());
CollectionType.php:      ->setAttribute('allow_add', $options['allow_add'])
CollectionType.php:      ->setAttribute('allow_delete', $options['allow_delete'])
DateTimeType.php:        ->setAttribute('widget', $options['widget']);
DateType.php:            ->setAttribute('formatter', $formatter)
DateType.php:            ->setAttribute('widget', $options['widget']);
FormType.php:            ->setAttribute('virtual', $options['virtual'])
MoneyType.php:           ->setAttribute('currency', $options['currency'])
PasswordType.php:        ->setAttribute('always_empty', $options['always_empty']);
RadioType.php:           ->setAttribute('value', $options['value'])
TimeType.php:            ->setAttribute('widget', $options['widget'])
TimeType.php:            ->setAttribute('with_seconds', $options['with_seconds'])


回答3:

See my answer here: https://stackoverflow.com/a/41020474/5758328

You simply need to use

{% dump %}

and all variables available in the template will be dumped to the profiler



回答4:

You can pull all of the ones out of the original file, and only overload the ones that you need:

vendor/symfony/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig


标签: symfony twig