我有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
的一部分,其他Field
允许设置属性,而Field
中的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>
...
答案 0 :(得分:4)
在SilverStripe 3.4中,$AttributesHTML
变量未在SelectionGroup_Item
使用的默认模板中调用。
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
变量。
为此,我们在SelectionGroup_Item.ss
目录中创建了一个mysite/templates/includes
文件。
<强> mysite的/模板/包括/ 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
以便系统清除它的缓存并找到这个新模板。