From 65e2c4b53dd17830f812f0f6fcfcd13ea5f6c04d Mon Sep 17 00:00:00 2001 From: Hao Sun Date: Wed, 15 Feb 2023 14:05:16 +0800 Subject: [PATCH] * zin: fix directive in block not work --- zin/core/wg.class.php | 62 +++++++++++++++++++++++-------------------- zin/wg/btn/v1.php | 9 ++++--- zin/wg/icon/v1.php | 9 ++++--- 3 files changed, 45 insertions(+), 35 deletions(-) diff --git a/zin/core/wg.class.php b/zin/core/wg.class.php index 29e1b735a1..63a8b77e3a 100644 --- a/zin/core/wg.class.php +++ b/zin/core/wg.class.php @@ -70,6 +70,8 @@ class wg return $this; } + protected function created() {} + protected function buildBefore() { return $this->block('before'); @@ -85,39 +87,34 @@ class wg return $this->children(); } - public function add($item) + protected function onAddBlock($child, $name) { - if($item === NULL) return $this; + return $child; + } + + protected function onAddChild($child) + { + return $child; + } + + public function add($item, $blockName = 'children') + { + if($item === NULL || is_bool($item)) return $this; if(is_array($item)) { - foreach($item as $child) $this->add($child); + foreach($item as $child) $this->add($child, $blockName); return $this; } - if($item instanceof wg) $this->addChild($item); - elseif(is_string($item)) $this->addChild(htmlentities($item)); - elseif(isDirective($item)) $this->directive($item); - else $this->addChild(htmlentities(strval($item))); + if($item instanceof wg) $this->addToBlock($blockName, $item); + elseif(is_string($item)) $this->addToBlock($blockName, htmlentities($item)); + elseif(isDirective($item)) $this->directive($item, $blockName); + else $this->addToBlock($blockName, htmlentities(strval($item))); return $this; } - public function addChild($child) - { - return $this->addToBlock('children', $child); - } - - public function addBefore($child) - { - $this->addToBlock('before', $child); - } - - public function addAfter($child) - { - $this->addToBlock('after', $child); - } - public function addToBlock($name, $child = NULL) { if(is_array($name)) @@ -139,6 +136,10 @@ class wg if($child instanceof wg) $child->parent = $this; + $result = $name === 'children' ? $this->onAddChild($child) : $this->onAddBlock($child, $name); + if($result === false) return; + if($result !== NULL && $result !== true) $child = $result; + if(isset($this->blocks[$name])) $this->blocks[$name][] = $child; else $this->blocks[$name] = array($child); } @@ -157,7 +158,7 @@ class wg * Apply directive * @param object $directive */ - public function directive($directive) + public function directive($directive, $blockName) { $data = $directive->data; $type = $directive->type; @@ -184,17 +185,20 @@ class wg } if($type === 'html') { - $this->addChild($data); + $this->addToBlock($blockName, $data); return; } if($type === 'text') { - $this->addChild(htmlspecialchars($data)); + $this->addToBlock($blockName, htmlspecialchars($data)); return; } if($type === 'block') { - $this->addToBlock($data); + foreach($data as $blockName => $blockChildren) + { + $this->add($blockChildren, $blockName); + } return; } } @@ -236,7 +240,7 @@ class wg } } - protected function created() {} + protected function onCreated() {} protected function toJsonData() { @@ -244,7 +248,7 @@ class wg $data['props'] = $this->props->toJsonData(); $data['type'] = get_called_class(); - if(strpos($data['$type'], 'zin\\') === 0) $data['$type'] = substr($data['$type'], 4); + if(strpos($data['type'], 'zin\\') === 0) $data['type'] = substr($data['type'], 4); $data['blocks'] = array(); foreach($this->blocks as $key => $value) @@ -297,7 +301,7 @@ class wg if(is_array($child)) { - $html[] = static::renderToHtml($child, ); + $html[] = static::renderToHtml($child); } elseif($child instanceof wg) { diff --git a/zin/wg/btn/v1.php b/zin/wg/btn/v1.php index b3ea40d1ac..14d681d82e 100755 --- a/zin/wg/btn/v1.php +++ b/zin/wg/btn/v1.php @@ -7,10 +7,13 @@ class btn extends wg { static $defineProps = 'type,icon,text,square,disabled,active,url,target,size,trailingIcon,caret,hint,btnType'; - public function addChild($child) + public function onAddChild($child) { - if(is_string($child) && !$this->props->has('text')) $this->props->set('text', $child); - else parent::addChild($child); + if(is_string($child) && !$this->props->has('text')) + { + $this->props->set('text', $child); + return false; + } } /** diff --git a/zin/wg/icon/v1.php b/zin/wg/icon/v1.php index f931296b17..76fd8fec3b 100644 --- a/zin/wg/icon/v1.php +++ b/zin/wg/icon/v1.php @@ -16,9 +16,12 @@ class icon extends wg ); } - public function addChild($child) + public function onAddChild($child) { - if(is_string($child) && !$this->props->has('name')) $this->props->set('name', $child); - else parent::addChild($child); + if(is_string($child) && !$this->props->has('name')) + { + $this->props->set('name', $child); + return false; + } } }