SilverStripe模板 - 如何访问无密钥值

时间:2015-03-21 13:05:36

标签: silverstripe

如何将每个SS模板的二维数组显示为html表?由于Child / Row只有值和没有键,所以我很难为每个模板显示主题。如何在模板中调用主题或者您将如何操作?

array showItems

0 =
    0 = Title
    1 = Text
    ...
1 =
    0 = Title
    1 = Text
    ...
...

PHP

public function showItems() {
    $result = ArrayList::create();
    $table = $this->getItems();
    foreach ($table as $row) {
        $r = DataObject::create($row);
        $result->push($r);
    }
    return $result;
}

模板:

<% if $showItems %>
    <table>
    <% loop $showItems %> // loop for rows
        <tr>
        <% loop $Children %> // loop for cells
            <td>$Me</td>
        <% end_loop %>
        </tr>
    <% end_loop %>
    </table>
<% end_if %>

1 个答案:

答案 0 :(得分:0)

每行都使用ArrayData,但如您知道每列的含义,我会为其添加密钥,例如:使用array_combine()

未测试:

public function showItems() {
    $result = ArrayList::create();
    $table = $this->getItems();
    $keys = array('ID', 'Foo', 'Bar');
    foreach ($table as $row) {
        $r = ArrayData::create(array_combine($keys, $row));
        $result->push($r);
    }
    return $result;
}

另一种可能性:

public function showItems() {
    $result = ArrayList::create();
    $table = $this->getItems();

    foreach ($table as $row) {
        $r = ArrayList::create($row)); //you can oop over an ArrayList
        $result->push($r);
    }
    return $result;
}

您可以在模板中执行以下操作:

<% if $showItems %>
    <table>
    <% loop $showItems %> // loop for rows
        <tr>
            <td>$ID - $Foo</td>
            <td>$Bar</td>
        </tr>
    <% end_loop %>
    </table>
<% end_if %>

另一种可能性:

public function showItems() {
    $result = ArrayList::create();
    $table = $this->getItems();

    foreach ($table as $row) {
        $r = ArrayData::create(array('Row' => ArrayList::create($row)))); //you can oop over an ArrayList
        $result->push($r);
    }
    return $result;
}

<% if $showItems %>
    <table>
    <% loop $showItems %> // loop for rows
        <tr>
        <% loop $Row %>
            <td>$Me</td> //dunno if that really works...
        <% end_loop %>
        </tr>
    <% end_loop %>
    </table>
<% end_if %>