我有一个表单集合可以添加多个"路由"在编辑页面上。出于视觉目的,我希望这是一个水平的输入列表。所以我已经自定义了collection_widget,它运行得很好,它与jquery集成以添加和删除记录等。而且非常重要的是,每个列上面都有标题,并带有帮助文本弹出窗口。
但是,如果记录还没有任何路线,除了添加按钮(即必要的标题消失)之外什么也没有显示。如果我&#34;添加&#34;一个新的路线,它添加原型行,但它不添加标题标题。您将看到我如何创建下面的<thead>
,而我根本不了解可用的变量,知道如何迭代可用字段来获取名称,即使有还没有记录。
那么如何循环遍历所有行,并为每个输入获取标签,以便我可以创建标题?即使没有记录数据。
fields.html.twig
{% block collection_widget %}
{% spaceless %}
{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': block('collection_item_widget') }) %}
{% endif %}
{# We add the collection class to encapsulate the whole container #}
{% set attr = attr|merge({'class': (attr.class|default('') ~ ' collection')|trim}) %}
<div {{ block('widget_container_attributes') }}>
{{ form_errors(form) }}
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
{% set fieldNum = 1 %}
{% for rows in form %}
{# Only Loop through the headers once, regardless of how many rows there are. #}
{% if fieldNum == 1 %}
{% for row in rows %}
{# row.vars.label #}
<td class="heading{{ fieldNum }} heading_{{ row.vars.name }}">
<h4 class="collectionHeading">
{{ form_label(row) }}
</h4>
</td>
{% endfor %}
{% endif %}
{% set fieldNum = fieldNum + 1 %}
{% endfor %}
{# for delete row #}
<td class="heading{{ fieldNum }} heading_action"> </td>
</tr>
</thead>
<tbody>
{% for prototype in form %}
{{ block('collection_item_widget') }}
{% endfor %}
</tbody>
<tfoot>
<tr>
<td colspan="{{ fieldNum }}">
<div class="actionContainer">
<span class="action addCollection btn btn-primary btn-xs" title="Add"><span class="glyphicon glyphicon-plus"></span> Add</span>
</div>
</td>
</tr>
</tfoot>
</table>
{{ form_rest(form) }}
</div>
<div class="clear"></div>
{% endspaceless %}
{% endblock collection_widget %}
{% block collection_item_widget %}
{% spaceless %}
<tr class="collectionEntity">
{% set fieldNum = 1 %}
{% for row in prototype %}
<td class="field{{ fieldNum }} field_{{ row.vars.name }}">
{{ form_widget(row) }}
</td>
{% set fieldNum = fieldNum + 1 %}
{% endfor %}
<td class="field{{ fieldNum }} field_action">
<span class="action removeCollection btn btn-default glyphicon glyphicon-trash" title="Remove"></span>
</td>
</tr>
{% endspaceless %}
{% endblock collection_item_widget %}
输出HTML
<div id="contentSeoEditType_routing" data-prototype=" <tr class="collectionEntity"><td class="field1 field_furl"><div class="input-group"><span class="input-group-addon">/</span><input type="text"
id="contentSeoEditType_routing___name___furl" name="contentSeoEditType[routing][__name__][furl]" required="required" style="min-width: 200px" class="form-control inputType-text"
/></div></td><td class="field2 field_flagPrimary"><input type="radio"
id="contentSeoEditType_routing___name___flagPrimary" name="contentSeoEditType[routing][__name__][flagPrimary]" class="radioExclusive form-control inputType-checkbox" data-radioexclusive="routingFlagPrimary"
value="1" /></td><td class="field3 field_action"><span class="action removeCollection btn btn-default glyphicon glyphicon-trash" title="Remove"></span></td></tr>" class="collection">
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
<td class="heading1 heading_furl">
<h4 class="collectionHeading">
<label for="contentSeoEditType_routing_0_furl" class="required">Friendy URL</label><span class="required" title="This field is Required">*</span><span rel="popover" data-target=".popoverContent.help-contentSeoEditType_routing_0_furl" data-placement="right" class="popoverButton help-contentSeoEditType_routing_0_furl glyphicon glyphicon-question-sign" data-original-title="" title=""></span>
<div class="popoverContent help-contentSeoEditType_routing_0_furl"><em>Versioning:</em> Friendly URLs are <strong>not</strong> managed by the versioning system. Changes to the route will effect both the live and staging website. However, you can create extra aliases for testing and then change which is the primary route when you are ready to make them live.</div>
</h4>
</td>
<td class="heading1 heading_flagPrimary">
<h4 class="collectionHeading">
<span rel="popover" data-target=".popoverContent.help-contentSeoEditType_routing_0_flagPrimary" data-placement="right" class="popoverButton help-contentSeoEditType_routing_0_flagPrimary glyphicon glyphicon-question-sign" data-original-title="" title=""></span>
<div class="popoverContent help-contentSeoEditType_routing_0_flagPrimary"><em>Primary Route:</em> A page will have one primary Friendly URL. Additional aliases will redirect back to the primary URL.</div>
</h4>
</td>
<td class="heading3 heading_action"> </td>
</tr>
</thead>
<tbody>
<tr class="collectionEntity">
<td class="field1 field_furl">
<div class="input-group"><span class="input-group-addon">/</span><input type="text" id="contentSeoEditType_routing_0_furl" name="contentSeoEditType[routing][0][furl]" required="required" style="min-width: 200px" class="form-control inputType-text" value="Turkish-We-Love-You"></div>
</td>
<td class="field2 field_flagPrimary"><input type="radio" id="contentSeoEditType_routing_0_flagPrimary" name="contentSeoEditType[routing][0][flagPrimary]" class="radioExclusive form-control inputType-checkbox" data-radioexclusive="routingFlagPrimary" value="1" checked="checked"></td>
<td class="field3 field_action"><span class="action removeCollection btn btn-default glyphicon glyphicon-trash" title="Remove"></span></td>
</tr>
<tr class="collectionEntity">
<td class="field1 field_furl">
<div class="input-group"><span class="input-group-addon">/</span><input type="text" id="contentSeoEditType_routing_1_furl" name="contentSeoEditType[routing][1][furl]" required="required" style="min-width: 200px" class="form-control inputType-text" value="A-Dog-Named-Turkish"></div>
</td>
<td class="field2 field_flagPrimary"><input type="radio" id="contentSeoEditType_routing_1_flagPrimary" name="contentSeoEditType[routing][1][flagPrimary]" class="radioExclusive form-control inputType-checkbox" data-radioexclusive="routingFlagPrimary" value="1"></td>
<td class="field3 field_action"><span class="action removeCollection btn btn-default glyphicon glyphicon-trash" title="Remove"></span></td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">
<div class="actionContainer"><span class="action addCollection btn btn-primary btn-xs" title="Add"><span class="glyphicon glyphicon-plus"></span> Add</span></div>
</td>
</tr>
</tfoot>
</table>
</div>
对于在表单模板自定义中基本无法执行任何操作的情况,这再次让人感到沮丧。通过文档和迭代_context
:
<ol>
{% for key, value in _context %}
<li>{{ key }} :
{% if value is not iterable%}
{{ value }}
{% else %}
{# WARNING! This causes the page to go blank white with no error. #}
{{ dump(value) }}
{% endif %}
</li>
{% endfor %}
</ol>
但这是一个手动黑客,并且做得很少,因为如果元素是一个对象(当你真的想知道的时候),你就不能转储它的价值(它打破了页面,空白,没有错误,这是我迷失了一天的另一个兔子洞)。如果我创建嵌套for循环,他们仍然不会打印出很多有用的价值信息。
仅仅因为变量存在,它在某些块上下文中有时是空的而不是其他块。我不知道这些块如何相互关联或者变量如何在它们之间传递,例如
{% block form_label %}
{{ parent() }}
{% if required is not empty %}
<span class="required" title="This field is Required">*</span>
{% endif %}
{{ block('form_help') }}
{% endblock %}
{# Custom Block for Help #}
{% block form_help %}
{% if help is not null %}
<span rel="popover" data-target=".popoverContent.help-{{ id }}" data-placement="right" class="popoverButton help-{{ id }} glyphicon glyphicon-question-sign"></span>
<div class="popoverContent help-{{ id }}">{{ help | raw }}</div>
{% endif %}
{% endblock %}
在某些情况下,我可以致电{{ block('form_help) }}
,但是在其他情况下(例如来自{% block collection_widget %}
它没有。我可以在{{_context
打印{% block form_help %}
1}}它会显示help
变量,但它是空的。为什么?
有一些文档通过引用myFieldName
字段(即vars
form.myFieldName.vars.publicVariable
的字段的任何public variable
<label for="{{ form.name.vars.id }}"
class="{{ form.name.vars.required ? 'required' : '' }}">
{{ form.name.vars.label }}
</label>
其他变量可用于不同的字段类型,例如collection类型有一个prototype变量,其中包含集合外观的模板。
{# this probably only works in your own template #}
{{ form_row(form.emails.vars.prototype) }}
{# this appears to work in the fields.html.twig master block template #}
{{ form_row(prototype) }}
但这并不能帮助我弄清楚如何访问字段标签来添加标题。我也不明白这些块如何相互来回传递变量,例如: