From 83ad410555c664a1282ecda8a3f2030e60bdcb7c Mon Sep 17 00:00:00 2001 From: mychidarko Date: Fri, 17 Feb 2023 17:30:38 +0000 Subject: [PATCH] fix: patch up @include error --- src/UI/Core.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/UI/Core.php b/src/UI/Core.php index ed5fbaa..090c7ee 100644 --- a/src/UI/Core.php +++ b/src/UI/Core.php @@ -98,8 +98,7 @@ protected static function buildComponent(Component $component, array $config, bo Core::createElement('script', [], [' window._leafUIConfig.methods = ' . json_encode(array_unique(static::$componentMethods)) . '; window._leafUIConfig.components = ' . json_encode(static::$components) . '; - ']) . - Core::init() . '', + ']) . Core::init() . '', static::compileTemplate($component->render(), static::$state[$component->key]) ), 'state' => $pageState, @@ -109,7 +108,7 @@ protected static function buildComponent(Component $component, array $config, bo $pageState = []; static::$state[$component->key] = array_merge(static::$state[$component->key], get_class_vars($component::class)); $parsedComponent = static::compileTemplate($component->render(), static::$state[$component->key]); - + foreach (array_values(static::$state) as $key => $value) { $pageState = array_merge($pageState, $value); } @@ -144,7 +143,7 @@ public static function view(string $filename): string throw new \JsonException("$filename not found!"); } - return file_get_contents($filename); + return static::compileTemplate(file_get_contents($filename)); } /** @@ -153,6 +152,12 @@ public static function view(string $filename): string */ public static function compileTemplate(string $rawText, array $state = []): string { + if (!$state) { + foreach (array_values(static::$state) as $key => $value) { + $state = array_merge($state, $value); + } + } + $compiled = preg_replace_callback('/{{(.*?)}}/', function ($matches) use ($state) { return $state[ltrim(trim($matches[1]), '$')] ?? trigger_error($matches[1] . ' is not defined', E_USER_ERROR); }, $rawText); @@ -212,6 +217,10 @@ public static function compileTemplate(string $rawText, array $state = []): stri return ""; }, $compiled); + $compiled = preg_replace_callback('/@loop\([\s\S]*?\)\s*[\s\S]*@endloop\s*/', function ($matches) { + return $matches[0]; + }, $compiled); + $compiled = preg_replace_callback('/@case\((.*?)\)/', function ($matches) { return ""; }, $compiled);