From a504cb1b5ce23e45d24660d7e671a62269561756 Mon Sep 17 00:00:00 2001 From: topnuomi <1130395124@qq.com> Date: Mon, 29 Jul 2024 15:11:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0README=E3=80=81=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 190 ++++++++++++++++++++++++- engine/Engine.php | 4 +- {engine => extend}/Article.php | 0 main.php | 8 +- test_dot.php | 71 --------- compile/result.php => tmp_compiled.php | 0 {templates => views}/base.html | 0 {templates => views}/footer.html | 0 {templates => views}/index.html | 0 9 files changed, 195 insertions(+), 78 deletions(-) rename {engine => extend}/Article.php (100%) delete mode 100644 test_dot.php rename compile/result.php => tmp_compiled.php (100%) rename {templates => views}/base.html (100%) rename {templates => views}/footer.html (100%) rename {templates => views}/index.html (100%) diff --git a/README.md b/README.md index 8c6576f..5b565d7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,190 @@ # Template-engine -### 此处仅作示例 + +### 模板继承 +模板继承通过extend标签与block标签配合使用实现。 +一个最简单的继承 +``` +// Base/layout.html(父级模板) + + + + + + Title + + + + + + +// Index/index.html + + + +

内容主体

+
+``` +### 模板标签 +内置一些常用标签 +1. php + +php标签。此标签为闭合标签,标签内的内容将被解析为原生php代码执行。 + +``` + + echo '你好'; + +``` + +2. if + +if标签。此标签为闭合标签,condition属性为if的条件,属性列表:condition。 + +``` + + // do something... + +``` + +3. else + +else标签。此标签为自闭合标签,可选属性condition,存在condition属性则被解析为elseif,属性列表:condition(可选)。 + +``` + + // do something... + + // do something... + + + + // do something... + + // do something... + +``` + +4. loop + +循环标签。此标签为闭合标签,属性列表:name、id、key(可选)。 + +``` + + {item.id} + + + + {i}、{item.id} + +``` + +5. assign + +赋值标签,在模板中创建新的php变量。此标签为自闭合标签,属性列表:name、value。 + +``` + +``` + +6. original + +此标签为闭合标签。original标签中的内容不会被编译。 + +``` + + + {item.id} + + +``` +上例,loop标签会被原样输出。 + +7. switch + +此标签为闭合标签。 + +``` + + + + $name的值为0 + + + $name的值为1 + + 默认 + +``` + +8. include + +在当前模板中加载其他模板文件。 + +9. 变量、函数调用 + +输出 +``` +{$username} +{$user.name} +{$user['name']} +``` +函数调用 +``` +{:mb_substr($username, 0, 3, 'utf8')} +{$username|mb_substr=0,3,'utf8'} +``` + +### 自定义标签 +新建自定义标签库类文件Extend.php,可选择继承TagLib类(支持点语法)。 +#### 闭合标签 +``` +class Extend extends TagLib +{ + public $tags = [ + 'test' => ['attr' => 'start,length,id', 'close' => 1] + ]; + + public function _test($attr, $content) + { + $parse = ''; + $parse .= $content; + $parse .= ''; + return $parse; + } +} +``` +类创建完成后,调用Engine中的loadTaglib方法加载Extend类 +``` +$config = [ + 'ext' => 'html', + 'dir' => './templates/', + 'left' => '<', + 'right' => '>', +]; +$engine = Engine::instance($config); +$engine->loadTaglib('extend', Extend::class); +``` +添加完成后即可在模板中使用 +``` + + {$test} + +``` +#### 自闭合标签 +添加一个描述 +``` +'say' => ['attr' => 'what', 'close' => 0] +``` +新建_say方法 +``` +public function _say($attr) +{ + return ""; +} +``` +模板调用 +``` + +``` \ No newline at end of file diff --git a/engine/Engine.php b/engine/Engine.php index 3186a1c..381cb0c 100644 --- a/engine/Engine.php +++ b/engine/Engine.php @@ -81,7 +81,7 @@ class Engine $blockPattern = '/' . $this->left . 'block\s+name[\s\S]*?=[\s\S]*?[\'"](.*?)[\'"][\s\S]*?' . $this->right; $blockPattern .= '([\s\S]*?)' . $this->left . '\/block' . $this->right . '/is'; // 获得被继承的模板内容 - $file = $this->config['dir'] . $matches[1] . '.' . ltrim($this->config['ext'], '.'); + $file = $this->config['path'] . $matches[1] . '.' . ltrim($this->config['ext'], '.'); $extendFileContent = null; if (file_exists($file)) { $extendFileContent = file_get_contents($file); @@ -128,7 +128,7 @@ class Engine $pattern = '/' . $this->left . 'include\s+file[\s\S]*?=[\s\S]*?[\'"](.*?)[\'"][\s\S]*?\/' . $this->right . '/is'; $template = preg_replace_callback($pattern, function ($result) { $string = null; - $file = $this->config['dir'] . $result[1] . '.' . ltrim($this->config['ext'], '.'); + $file = $this->config['path'] . $result[1] . '.' . ltrim($this->config['ext'], '.'); if (file_exists($file)) { $string = file_get_contents($file); } diff --git a/engine/Article.php b/extend/Article.php similarity index 100% rename from engine/Article.php rename to extend/Article.php diff --git a/main.php b/main.php index 2260f99..5c7c0a7 100644 --- a/main.php +++ b/main.php @@ -3,14 +3,14 @@ require 'engine/Engine.php'; require 'engine/TagLib.php'; require 'engine/Tags.php'; -require 'engine/Article.php'; +require 'extend/Article.php'; $t1 = microtime(true); // 获取模板引擎实例 $config = [ 'ext' => 'html', - 'dir' => './templates/', + 'path' => './views/', 'left' => '<', 'right' => '>', ]; @@ -18,10 +18,10 @@ $engine = Engine::instance($config); // 加载自定义标签库 $engine->loadTaglib('article', Article::class); // 读取模板内容 -$template = file_get_contents('./templates/index.html'); +$template = file_get_contents('./views/index.html'); // 编译并写入 $content = $engine->compile($template); -file_put_contents('./compile/result.php', $content); +file_put_contents('./tmp_compiled.php', $content); $t2 = microtime(true); diff --git a/test_dot.php b/test_dot.php deleted file mode 100644 index 1d61e8f..0000000 --- a/test_dot.php +++ /dev/null @@ -1,71 +0,0 @@ -_parseDotSyntax($variable); - } - - /** - * 解析点语法 - * @param string $variable - * @return string - */ - private function _parseDotSyntax($variable) - { - // 处理.语法(仅数组或已实现数组访问接口的对象) - return preg_replace_callback("/\.([a-zA-Z0-9_-]*)/", function ($match) { - if (isset($match[1]) && $match[1]) { - return '[' . (is_numeric($match[1]) ? $match[1] : "'{$match[1]}'") . ']'; - } else { - return null; - } - }, $variable); - } -} - -$template = new Template(); - -echo $template->parseVariable('$arr.0.id|md5|substr=0,1|json_encode|date="Y-m-d",###|test=1,2,###,3,4'); -echo PHP_EOL; -echo $template->parseVariable('$arr.id'); -echo PHP_EOL; -echo $template->parseVariable("\$arr['id']"); -echo PHP_EOL; -echo $template->parseVariable('$arr.article_id|intval'); -echo PHP_EOL; -echo $template->parseVariable("\$arr['article_id']|intval|strtotime"); diff --git a/compile/result.php b/tmp_compiled.php similarity index 100% rename from compile/result.php rename to tmp_compiled.php diff --git a/templates/base.html b/views/base.html similarity index 100% rename from templates/base.html rename to views/base.html diff --git a/templates/footer.html b/views/footer.html similarity index 100% rename from templates/footer.html rename to views/footer.html diff --git a/templates/index.html b/views/index.html similarity index 100% rename from templates/index.html rename to views/index.html