更新README、目录名

This commit is contained in:
TOP糯米 2024-07-29 15:11:13 +08:00
parent a9ad41e054
commit a504cb1b5c
9 changed files with 195 additions and 78 deletions

190
README.md
View File

@ -1,2 +1,190 @@
# Template-engine # Template-engine
### 此处仅作示例
### 模板继承
模板继承通过extend标签与block标签配合使用实现。
一个最简单的继承
```
// Base/layout.html父级模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<block name="body"></block>
</body>
</html>
// Index/index.html
<extend file="Base/layout" />
<block name="body">
<h3>内容主体</h3>
</block>
```
### 模板标签
内置一些常用标签
1. php
php标签。此标签为闭合标签标签内的内容将被解析为原生php代码执行。
```
<php>
echo '你好';
</php>
```
2. if
if标签。此标签为闭合标签condition属性为if的条件属性列表condition。
```
<if condition="$age eq 10">
// do something...
</if>
```
3. else
else标签。此标签为自闭合标签可选属性condition存在condition属性则被解析为elseif属性列表condition可选
```
<if condition="$age eq 10">
// do something...
<else />
// do something...
</if>
<if condition="$age eq 10">
// do something...
<else condition="$age eq 20" />
// do something...
</if>
```
4. loop
循环标签。此标签为闭合标签属性列表name、id、key可选
```
<loop name="lists" id="item">
{item.id}
</loop>
<loop name="lists" id="item" key="i">
{i}、{item.id}
</loop>
```
5. assign
赋值标签在模板中创建新的php变量。此标签为自闭合标签属性列表name、value。
```
<assign name="username" value="TOP糯米" />
```
6. original
此标签为闭合标签。original标签中的内容不会被编译。
```
<original>
<loop name="lists" id="item">
{item.id}
</loop>
</original>
```
上例loop标签会被原样输出。
7. switch
此标签为闭合标签。
```
<assign name="value" value="1" />
<switch name="value">
<case value="0">
$name的值为0
</case>
<case value="1">
$name的值为1
</case>
<case>默认</case>
</switch>
```
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 = '<?php for ($' . $attr['id'] . ' = ' . $attr['start'] . '; $' . $attr['id'];
$parse .= ' < ' . $attr['start'] . ' + ' . $attr['length'] . '; ';
$parse .= '$' . $attr['id'] . '++): ?>';
$parse .= $content;
$parse .= '<?php endfor; ?>';
return $parse;
}
}
```
类创建完成后调用Engine中的loadTaglib方法加载Extend类
```
$config = [
'ext' => 'html',
'dir' => './templates/',
'left' => '<',
'right' => '>',
];
$engine = Engine::instance($config);
$engine->loadTaglib('extend', Extend::class);
```
添加完成后即可在模板中使用
```
<extend:test start="1" length="10" id="test">
{$test}
</extend:test>
```
#### 自闭合标签
添加一个描述
```
'say' => ['attr' => 'what', 'close' => 0]
```
新建_say方法
```
public function _say($attr)
{
return "<?php echo '{$attr['what']}'; ?>";
}
```
模板调用
```
<extend:say what="Hello world!" />
```

View File

@ -81,7 +81,7 @@ class Engine
$blockPattern = '/' . $this->left . 'block\s+name[\s\S]*?=[\s\S]*?[\'"](.*?)[\'"][\s\S]*?' . $this->right; $blockPattern = '/' . $this->left . 'block\s+name[\s\S]*?=[\s\S]*?[\'"](.*?)[\'"][\s\S]*?' . $this->right;
$blockPattern .= '([\s\S]*?)' . $this->left . '\/block' . $this->right . '/is'; $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; $extendFileContent = null;
if (file_exists($file)) { if (file_exists($file)) {
$extendFileContent = file_get_contents($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'; $pattern = '/' . $this->left . 'include\s+file[\s\S]*?=[\s\S]*?[\'"](.*?)[\'"][\s\S]*?\/' . $this->right . '/is';
$template = preg_replace_callback($pattern, function ($result) { $template = preg_replace_callback($pattern, function ($result) {
$string = null; $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)) { if (file_exists($file)) {
$string = file_get_contents($file); $string = file_get_contents($file);
} }

View File

@ -3,14 +3,14 @@
require 'engine/Engine.php'; require 'engine/Engine.php';
require 'engine/TagLib.php'; require 'engine/TagLib.php';
require 'engine/Tags.php'; require 'engine/Tags.php';
require 'engine/Article.php'; require 'extend/Article.php';
$t1 = microtime(true); $t1 = microtime(true);
// 获取模板引擎实例 // 获取模板引擎实例
$config = [ $config = [
'ext' => 'html', 'ext' => 'html',
'dir' => './templates/', 'path' => './views/',
'left' => '<', 'left' => '<',
'right' => '>', 'right' => '>',
]; ];
@ -18,10 +18,10 @@ $engine = Engine::instance($config);
// 加载自定义标签库 // 加载自定义标签库
$engine->loadTaglib('article', Article::class); $engine->loadTaglib('article', Article::class);
// 读取模板内容 // 读取模板内容
$template = file_get_contents('./templates/index.html'); $template = file_get_contents('./views/index.html');
// 编译并写入 // 编译并写入
$content = $engine->compile($template); $content = $engine->compile($template);
file_put_contents('./compile/result.php', $content); file_put_contents('./tmp_compiled.php', $content);
$t2 = microtime(true); $t2 = microtime(true);

View File

@ -1,71 +0,0 @@
<?php
class Template
{
/**
* 解析变量输出
* @param string $variable
* @return string
*/
public function parseVariable($variable)
{
// 处理|函数调用
if (strstr($variable, '|')) {
$functions = explode('|', $variable);
// 赋值初始执行结果
$executeResult = $functions[0];
// 只留下函数表达式
unset($functions[0]);
// 重置调用函数数组索引以便开始foreach循环
$functions = array_values($functions);
foreach ($functions as $function) {
$expParameters = explode('=', $function);
$functionName = $expParameters[0];
// 如果有带上参数,则进行参数处理,没有声明参数则直接将当前值作为函数的第一个参数
if (isset($expParameters[1])) {
$parameters = $expParameters[1];
// 如果有参数,则处理,同时将占位符###替换为上次解析结果
// 如果存在占位符,则直接替换,没有占位符则将当前执行结果作为函数的第一个参数
$invokeParameters = strstr($expParameters[1], '###')
? str_replace('###', $executeResult, $parameters)
: $executeResult . ',' . $parameters;
$executeResult = $functionName . '(' . $invokeParameters . ')';
} else {
$executeResult = $functionName . '(' . $executeResult . ')';
}
}
$variable = $executeResult;
}
return $this->_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");