* zin: fix directive in block not work

This commit is contained in:
Hao Sun
2023-02-15 14:05:16 +08:00
parent 75bfdc24a0
commit 65e2c4b53d
3 changed files with 45 additions and 35 deletions
+33 -29
View File
@@ -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)
{
+6 -3
View File
@@ -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;
}
}
/**
+6 -3
View File
@@ -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;
}
}
}