【问题标题】:Is there a complete list of variables that can be used in form_div_layout.html.twig?是否有可以在 form_div_layout.html.twig 中使用的完整变量列表?
【发布时间】:2012-02-28 16:15:27
【问题描述】:

我想获取 Symfony 表单主题文件 form_div_layout.html.twig 中可用的所有变量,我阅读了 Symfony 官方文档并在网上搜索,但找不到任何有用的信息,有人可以帮助我?

【问题讨论】:

  • 抱歉...您觉得我的回答有用吗?
  • 我也为此浪费了很多时间。

标签: symfony twig


【解决方案1】:

嗯,你可以通过迭代上下文来获取每个块中所有可用的变量:

{% 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 %}

如果你想使用你的,那么你必须覆盖实际呈现这些小部件的类,看看 AbtractType::buildView...

正如@Gregoire 建议的那样,您可以使用 1.5 版 (http://twig.sensiolabs.org/doc/functions/dump.html) 中的{{ dump(_context) }},但请注意它会打印大量信息。

【讨论】:

  • 您也可以在循环中添加{{ dump(value) }},以显示变量的内容。
  • @Gregoire 在 Symfony 2.5 中 {{ dump(value) }} 在值是对象的情况下使整个页面变白(显然已知内存泄漏)(这使得转储无用,因为那是我通常需要转储)。
【解决方案2】:

我最近遇到了同样的问题,因为在主题中工作时缺少可用变量(属性)的文档。最后,我通过在供应商文件夹中搜索我确实知道的变量(花了一段时间)找到了我的解决方案,看看还有什么可用的。

对我来说最好的地方是看这里:Symfony\Component\Form\Extension\Core\Type

基础类型,即 FieldType,通过 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 是只存在于集合类型中的属性,allow_add 和allow_delete 也是如此,参见同文件夹中的CollectionType。

在基本 FieldType 之后,这似乎是完整的列表。

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'])

【讨论】:

  • 谢谢,我会试试看如何使用它们。
  • collectionType中,我们如何迭代集合中包含的每个输入字段?你知道如何访问父元素或整个表单的一部分吗? See this question
【解决方案3】:

在这里查看我的答案:https://stackoverflow.com/a/41020474/5758328

你只需要使用

{% dump %}

模板中可用的所有变量都将转储到分析器

【讨论】:

    【解决方案4】:

    您可以从原始文件中提取所有文件,并且只有 overload 您需要的文件:

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

    【讨论】:

    • 实际上,问题是我有一个带有集合的表单,我希望集合中的小部件使用与父表单不同的另一个主题,我不知道如何存档。
    • 我尝试为自定义字段创建自定义主题,但小部件相互引用,我试图按照链覆盖每个但我迷路了。
    • 你能把你目前拥有的东西贴上来,看看我能不能帮你解决
    • 这就是我的意思,github.com/symfony/symfony-docs/issues/696,有人为嵌入式表单创建了一个主题,pastie.org/2522598,这正是我要找的。​​span>
    • 我看到他使用了变量'prototype','attr',还有其他我可以使用的吗?如果我想创建这样的主题,我应该知道什么?而“形式”是可遍历的,它的内容是什么?
    猜你喜欢
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多