Move column rendering into templates
authorMarcel Werk <burntime@woltlab.com>
Wed, 11 Sep 2024 13:37:52 +0000 (15:37 +0200)
committerMarcel Werk <burntime@woltlab.com>
Wed, 11 Sep 2024 13:37:52 +0000 (15:37 +0200)
com.woltlab.wcf/templates/shared_gridView.tpl
com.woltlab.wcf/templates/shared_gridViewRows.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/view/grid/AbstractGridView.class.php
wcfsetup/install/files/lib/system/view/grid/DatabaseObjectListGridView.class.php

index 328d58e383f3538c9660246ffa2763da513e8314..cd984ed4401a4eeb557383a77d43b7ee761ee020 100644 (file)
@@ -7,7 +7,15 @@
                <table class="table" id="{$view->getID()}_table">
                        <thead>
                                <tr>
-                                       {unsafe:$view->renderHeader()}
+                                       {foreach from=$view->getColumns() item='column'}
+                                               <th
+                                                       class="{$column->getClasses()}"
+                                                       data-id="{$column->getID()}"
+                                                       data-sortable="{$column->isSortable()}"
+                                               >
+                                                       {unsafe:$column->getLabel()}
+                                               </th>
+                                       {/foreach}
                                </td>
                        </thead>
                        <tbody>
diff --git a/com.woltlab.wcf/templates/shared_gridViewRows.tpl b/com.woltlab.wcf/templates/shared_gridViewRows.tpl
new file mode 100644 (file)
index 0000000..13b455f
--- /dev/null
@@ -0,0 +1,10 @@
+
+{foreach from=$view->getRows() item='row'}
+       <tr>
+               {foreach from=$view->getColumns() item='column'}
+                       <td class="{$column->getClasses()}">
+                               {unsafe:$view->renderColumn($column, $row)}
+                       </td>
+               {/foreach}
+       </tr>
+{/foreach}
index ee68dbe2c6f36d9115f11324452b97150eb753d6..9e23dbba0ebaf61eec8b3147b7c3f12c345d57f9 100644 (file)
@@ -48,46 +48,14 @@ abstract class AbstractGridView
         return WCF::getTPL()->fetch('shared_gridView', 'wcf', ['view' => $this], true);
     }
 
-    public function renderHeader(): string
-    {
-        $header = '';
-
-        foreach ($this->getColumns() as $column) {
-            $header .= <<<EOT
-                <th
-                    class="{$column->getClasses()}"
-                    data-id="{$column->getID()}"
-                    data-sortable="{$column->isSortable()}"
-                >{$column->getLabel()}</th>
-            EOT;
-        }
-
-        return $header;
-    }
-
     public function renderRows(): string
     {
-        $result = '';
-
-        foreach ($this->getRows() as $row) {
-            $result .= <<<EOT
-                <tr>
-            EOT;
-
-            foreach ($this->getColumns() as $column) {
-                $result .= <<<EOT
-                    <td class="{$column->getClasses()}">
-                        {$column->render($this->getData($row,$column->getID()),$row)}
-                    </td>
-                EOT;
-            }
-
-            $result .= <<<EOT
-                </tr>
-            EOT;
-        }
+        return WCF::getTPL()->fetch('shared_gridViewRows', 'wcf', ['view' => $this], true);
+    }
 
-        return $result;
+    public function renderColumn(GridViewColumn $column, mixed $row): string
+    {
+        return $column->render($this->getData($row, $column->getID()), $row);
     }
 
     protected function getData(mixed $row, string $identifer): mixed
@@ -95,7 +63,7 @@ abstract class AbstractGridView
         return $row[$identifer] ?? '';
     }
 
-    protected abstract function getRows(): array;
+    public abstract function getRows(): array;
 
     public abstract function countRows(): int;
 
index 5f44e981eb817576cb8dbf35cfa4c6323d695ff0..4cba1e49fa6bbae0b6ff5090665a6ee43b4c9d46 100644 (file)
@@ -13,7 +13,7 @@ abstract class DatabaseObjectListGridView extends AbstractGridView
     protected DatabaseObjectList $objectList;
     private int $objectCount;
 
-    protected function getRows(): array
+    public function getRows(): array
     {
         $this->getObjectList()->readObjects();