SF2 Twig:如何自定义 CollectionType 中嵌入的 CoolectionType 的渲染?

SF2 Twig : How to customize rendering of CoolectionType embedded in CollectionType?

我正在设计一个带有调查实体的应用程序。每个调查可以有多个 SurveyQuestion 实体(表单中的 CollectionType,属性 Survey->questions),每个 SurveyQuestion 可以有多个 SurveyAnswer 实体(SurveyQuestion->availableAnswers)。

表格在 SurveyType、QuestionType 和 AnswerType 中逻辑分布。

如何设置可用答案的模板片段?

我查看了 2 条线索:

首先,form_theme 定位特定类型:在调查表 twig

 {# Theming for questions #}
 {% form_theme form.questions 'LCHModuleBundle:Survey/form:question.html.twig' %}

 {# Theming for answers #}
{#{% form_theme form.availableAnswers 'LCHModuleBundle:Survey/form:answer.html.twig' %}#}

看起来无法访问 availableQuestions,因为它是问题集合中一项的字段。

二、块命名(根据this):

在调查表中 twig :

{% form_theme form with ['LCHModuleBundle:Survey/form:question.html.twig', 'LCHModuleBundle:Survey/form:answer.html.twig'] %}

在question.html.twig中:

{#Question list rendering#}
{% block _lchmodule_bundle_survey_type_questions_widget %}...{% endblock %}

问题类型:

class QuestionType extends AbstractType
{
    const NAME= "lchmodule_bundle_question_type";.

    public function getName()
    {
        return self::NAME;
    }

    /**
     * @return string
     */
    public function getBlockPrefix()
    {
        return self::NAME;
    }
}

在answer.html.twig中:

{% block _lchmodule_bundle_answer_type_availableAnswers_widget %}...{% endblock %}

在答案类型中:

class AnswerType extends AbstractType
{
    const NAME = "lchmodule_bundle_answer_type";
   /**
     * @return string
     */
    public function getName()
    {
        return self::NAME;
    }

    /**
     * @return string
     */
    public function getBlockPrefix() {
        return self::NAME;
    }
}

THeming 非常适合提问,但不是答案。我如何访问 availableQuestions 主题,因为它是 CollectionType 中的嵌入式 CollectionType?

谢谢你的灯,

尼古拉斯

在主窗体上,将您的 collection 项目打印为:

{% for collectionItem in form.collections %}
    {% include 'AppBundle:Blog:collection.item.html.twig' %}
{% endfor %}

这样收集项可以放在单独的 twig 文件中。现在在单独的树枝文件 (collection.item.html.twig) 中,定义您的 form_theme 或进行必要的更改。

希望这对您有所帮助。