diff --git a/webfiori/framework/ui/WebPage.php b/webfiori/framework/ui/WebPage.php index 254e3604..add4271b 100644 --- a/webfiori/framework/ui/WebPage.php +++ b/webfiori/framework/ui/WebPage.php @@ -756,11 +756,7 @@ public function removeChild($node) { * @since 1.0 */ public function render(bool $formatted = false, bool $returnResult = false) { - $this->beforeRenderCallbacks->insertionSort(false); - - foreach ($this->beforeRenderCallbacks as $callbackObj) { - $callbackObj->call($this); - } + $this->invokeBeforeRender(); if (!$returnResult) { $formatted = $formatted === true || (defined('WF_VERBOSE') && WF_VERBOSE); @@ -771,6 +767,21 @@ public function render(bool $formatted = false, bool $returnResult = false) { return $this->getDocument(); } + private function invokeBeforeRender(int $current = 0) { + $currentCount = count($this->beforeRenderCallbacks); + + if ($currentCount == 0 || $currentCount == $current) { + return; + } + $this->beforeRenderCallbacks->get($current)->call($this); + $newCount = count($this->beforeRenderCallbacks); + if ($newCount != $currentCount) { + $this->beforeRenderCallbacks->insertionSort(false); + $this->invokeBeforeRender(); + } else { + $this->invokeBeforeRender($current + 1); + } + } /** * Resets page attributes to default values. *