douqiao6563 2016-08-03 10:16
浏览 51
已采纳

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

I'm designing an app with a Survey entity. Each survey can have multiple SurveyQuestion entities (CollectionType in form, attribute Survey->questions) and each SurveyQuestion can have multiple SurveyAnswer entities (SurveyQuestion->availableAnswers).

Forms are logically spread in SurveyType, QuestionType and AnswerType.

How can I set template fragment for availableAnswers?

I looked around 2 leads :

First, form_theme targeting specific type : in Survey form 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' %}#}

Looks impossible to access availableQuestions because it's a field from one item of questions collection.

Second, block naming (according to this) :

In Survey form twig :

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

In question.html.twig :

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

In QuestionType :

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

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

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

In answer.html.twig :

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

In AnswerType :

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 is working great for questions, but not for answers. How can I access availableQuestions theming regarding the fact that it's an embedded CollectionType within an CollectionType?

Thanks for your lights,

Nicolas

  • 写回答

1条回答 默认 最新

  • dongyong6332 2016-08-04 08:55
    关注

    On the main form, print your collection items as :

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

    so that the colleciton items can be in separate twig file. Now in separate twig file (collection.item.html.twig), define your form_theme or do necessary changes.

    hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗