首页模板引擎View 引擎
View 引擎
Kuoqi\Core\View(99 行):模板编译管线 + 按需执行 + 缓存
本页内容
  1. 渲染管线
  2. 缓存机制
  3. 注意事项

渲染管线

#处理器标签
1IfView<if>/<elseif>/<else>
2PhpView<php>...</php>、data-php
3ForView<for>
4VolistView<volist>
5NovolistView<novolist>
6FuncView{:func()}
7EchoView{$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 · 返回首页