如何使用树枝中实体的现有数据预填充集合原型?

How to pre populate collection prototype with existing data from entity in twig?

我是 symfony 的新手,我已经从实体接收到数据到我的控制器并将它发送到我的树枝模板现在我想使用集合原型显示该数据以便我可以更新该数据,

我创建集合原型的js代码

custom.js

function addTagForm(collectionHolder, newLinkLi) {

    var prototype = collectionHolder.data('prototype');

    // get the new index
    var index = collectionHolder.data('index');

    // Replace '__name__' in the prototype's HTML to
    // instead be a number based on how many items we have
    var newForm = prototype.replace(/__name__/g, index);

     collectionHolder.data('index', index + 1);
    if(collectionHolder.data('index') <= 6){

         // Display the form in the page in an li, before the "Add a tag" link li
        var newFormLi = $('<li style="width:100%"></li>').append(newForm);

        newLinkLi.before(newFormLi);

        //alert(newFormLi);
    }else{

    $(newLinkLi).remove();

    }

}

});

我的树枝将其渲染成 html

 <div class="form-group"> 
    <label class="col-sm-2 control-label required" for="product_about_product">Reference List</label><div class="col-sm-10">
   <ul class="c4" data-prototype="{{ form_widget(form.ReferenceLists.vars.prototype)|e }}">    
                    </ul>
                    </div>
                    </div>

Prototype 用于通过 javascript 动态创建新的表单项。要显示现有的集合项,您需要遍历集合类型的子表单:

<ul>
{% for child in form.ReferenceLists %}
    <li>{{ form_widget(child) }}</li>
{% endfor
</ul>