首页›模板引擎›View 引擎
View 引擎
Kuoqi\Core\View(99 行):模板编译管线 + 按需执行 + 缓存
本页内容
- 渲染管线
- 缓存机制
- 注意事项
渲染管线
| # | 处理器 | 标签 |
|---|---|---|
| 1 | IfView | <if>/<elseif>/<else> |
| 2 | PhpView | <php>...</php>、data-php |
| 3 | ForView | <for> |
| 4 | VolistView | <volist> |
| 5 | NovolistView | <novolist> |
| 6 | FuncView | {:func()} |
| 7 | EchoView | {$var} |
按需执行(性能优化)
// 编译前先 strpos 扫描,只运行出现的标签引擎
$hasIf = (strpos($this->__stream, "<if ") !== false);
$hasPhp = (strpos($this->__stream, "<php>") !== false);
$hasFor = (strpos($this->__stream, "<for ") !== false);
$hasVolist = (strpos($this->__stream, "<volist ") !== false);
$hasNovolist = (strpos($this->__stream, "<novolist ") !== false);
$hasFunc = (strpos($this->__stream, "{:") !== false);
$hasEcho = (preg_match("/{\\$[0-9a-zA-Z_]/", $this->__stream) > 0);
if($hasIf) $this->__stream = View\IfView::execCode($this->__stream);
if($hasPhp) $this->__stream = View\PhpView::execCode($this->__stream);
// ... 按固定顺序执行缓存机制
- 模板内容 MD5 作为缓存 ID:Public/Runtime/Template/{md5}.template
- 缓存文件存在则直接 include(跳过全部正则编译)
- CACHE_TEMPLATE_STATUS=false(默认)时每次重新编译,改模板即时生效
- 模板修改后如不生效:php kuoqi clear 或删除 Public/Runtime/Template
变量注入
// toStream 中
ob_start();
foreach($this->__params as $k=>$v){ $$k = $v; } // 参数展开为独立变量
include($__file);
return trim(ob_get_clean());注意事项
参数 key 必须是合法 PHP 变量名(字母/数字/下划线);模板中 {$var} 依赖 isset 判断,变量已定义(含空串)就会输出。
KuoqiPHP v1.05 · 离线静态文档 · 界面基于 KuoqiUI ·
返回首页