PHP-SelectionGroup不添加额外的属性
作者:互联网
我有PHP代码:
SelectionGroup::create(
//...
SelectionGroup_Item::create(/*...*/),
SelectionGroup_Item::create(/*...*/),
//...
)
->addExtraClass("some-extra-class")
->setAttribute('ng-change','log(myModel)')
->setAttribute('ng-model','myModel')
和呈现的html看起来像:
...
<ul class="SelectionGroup field CompositeField selectiongroup some-extra-class nolabel">
...
</ul>
...
我要添加额外的类,为什么不添加我的额外属性?
此SelectionGroup是FieldList的一部分,其他字段允许设置属性,而SelectionGroup_Item中的字段可以设置其属性,例如:
FieldList::create([
HiddenField::create(...)->setAttribute("does","this work"),
SelectionGroup::create(
//...
SelectionGroup_Item::create('name',
FieldGroup::create(null,[
HiddenField::create(...)->setAttribute("maybe","it does")
])
),
SelectionGroup_Item::create(...),
//...
)
->addExtraClass("some-extra-class")
->setAttribute('ng-change','log(myModel)')
->setAttribute('ng-model','myModel')
])
呈现以下HTML:
...
<input type="hidden" ... does="this work" />
<ul class="SelectionGroup field CompositeField selectiongroup some-extra-class nolabel">
...
<input type="hidden" ... maybe="it does" />
...
</ul>
...
解决方法:
在SilverStripe 3.4中,SelectionGroup_Item使用的默认模板中未调用$AttributesHTML变量.
SelectionGroup_Item使用CompositeField模板(因为它是extends CompositeField
,并且在框架中没有设置自己的模板).
框架中当前的CompositeField模板在开始标记中不包含$AttributesHTML:
<$Tag class="CompositeField $extraClass <% if ColumnCount %>multicolumn<% end_if %>">
<% if $Tag == 'fieldset' && $Legend %>
<legend>$Legend</legend>
<% end_if %>
<% loop $FieldList %>
<% if $ColumnCount %>
<div class="column-{$ColumnCount} $FirstLast">
$Field
</div>
<% else %>
$Field
<% end_if %>
<% end_loop %>
</$Tag>
我们可以创建自己的SelectionGroup_Item模板或CompositeField来添加$AttributesHTML变量.
为此,我们在mysite / templates / includes目录中创建SelectionGroup_Item.ss文件.
mysite / templates / includes / SelectionGroup_Item.ss
<$Tag $AttributesHTML class="CompositeField $extraClass <% if ColumnCount %>multicolumn<% end_if %>">
<% if $Tag == 'fieldset' && $Legend %>
<legend>$Legend</legend>
<% end_if %>
<% loop $FieldList %>
<% if $ColumnCount %>
<div class="column-{$ColumnCount} $FirstLast">
$Field
</div>
<% else %>
$Field
<% end_if %>
<% end_loop %>
</$Tag>
创建此模板后,我们需要在页面URL中调用?flush = all,以使系统清除其缓存并找到此新模板.
标签:silverstripe,php 来源: https://codeday.me/bug/20191026/1939204.html