From 6d2196b860a63b3c8c1105d2bee475d564668b63 Mon Sep 17 00:00:00 2001 From: Hao Sun Date: Tue, 17 Jan 2023 10:13:15 +0800 Subject: [PATCH] * zin: add builder class for building html code --- zin/core/builder.class.php | 168 +++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 zin/core/builder.class.php diff --git a/zin/core/builder.class.php b/zin/core/builder.class.php new file mode 100644 index 0000000000..377d94cfb6 --- /dev/null +++ b/zin/core/builder.class.php @@ -0,0 +1,168 @@ +tag = $tag; + } + + public function before($content) + { + if(is_array($content)) $this->prefix = array_merge($this->prefix, $content); + else $this->prefix[] = $content; + return $this; + } + + public function after($content) + { + if(is_array($content)) $this->suffix = array_merge($this->suffix, $content); + else $this->suffix[] = $content; + return $this; + } + + public function empty() + { + $this->children = []; + return $this; + } + + public function append($content) + { + if(is_array($content)) $this->children = array_merge($this->children, $content); + else $this->children[] = $content; + return $this; + } + + public function prepend($content) + { + if(is_array($content)) $this->children = array_merge($content, $this->children); + else array_unshift($this->children, $content); + return $this; + } + + public function props($props, $reset = false) + { + if($reset) $this->propsStr = ''; + + $this->propsStr .= ' ' . strval($props); + return $this; + } + + public function js($code) + { + if(is_array($code)) $this->jsCode = array_merge($this->jsCode, $code); + else $this->jsCode[] = $code; + return $this; + } + + public function css($code) + { + if(is_array($code)) $this->cssCode = array_merge($this->cssCode, $code); + else $this->cssCode[] = $code; + return $this; + } + + public function importJs($jsFile) + { + if(is_array($jsFile)) $this->jsImports = array_merge($this->jsImports, $jsFile); + else $this->jsImports[] = $jsFile; + return $this; + } + + public function build() + { + $html = array(); + + if(!empty($this->cssImports)) + { + foreach($this->cssImports as $href) + { + if(!empty($href)) $html[] = ""; + } + } + + if(!empty($this->cssCode)) + { + $cssCode = ''; + foreach($this->cssCode as $css) + { + if(!empty($css)) $cssCode .= $css; + } + if(!empty($cssCode)) $html[] = ""; + } + + if(!empty($this->prefix)) $html = array_merge($html, $this->prefix); + + if(!empty($this->tag)) $html[] = "<$this->tag" . "$this->propsStr>"; + + if(!empty($this->children)) array_merge($html, $this->children); + + if(!empty($this->tag)) $html[] = "tag>"; + + if(!empty($this->suffix)) $html = array_merge($html, $this->suffix); + + $jsCode = ''; + if(!empty($this->jsVars)) + { + foreach($this->jsVars as $var => $val) + { + if(empty($var)) continue; + if(strpos($var, 'window.') === 0) $jsCode .= "$var=" . json_encode($val) . ';'; + else $jsCode .= "var $var=" . json_encode($val) . ';'; + } + } + if(!empty($this->jsImports)) + { + foreach($this->jsImports as $src) + { + if(!empty($src)) $html[] = ""; + } + } + if(!empty($this->jsCode)) + { + foreach($this->jsCode as $js) + { + if(!empty($js)) $jsCode .= $js; + } + } + if(!empty($jsCode)) $html[] = ""; + + return implode("\n", $html); + } + + /** + * Create html builder instance + * + * @param array $tag - Element tag name + * @return builder + */ + static public function new($tag = '') + { + return new builder($tag); + } +}