SelectionGroup不添加额外属性

时间:2016-08-10 01:37:16

标签: php silverstripe

我有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>
...

我的额外课程正在添加,为什么不添加我的额外属性?

SelectionGroupFieldList的一部分,其他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>
...

1 个答案:

答案 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以便系统清除它的缓存并找到这个新模板。