Z-PHP_v5

Z-PHP5 文档


视图模板

<h3>模板只是为了方便阅读,能够清晰的看懂模版的结构, 以及方便各个模板的组合和复用。使用另外的语法规则会引入额外的复杂度,增加使用者的学习成本,违背了此框架的初衷。有人认为需要为那些编写html的人员单独定制一套语法,我觉得与其额外增加一些稀奇古怪的语法规则,还不如使用原生的语法来的清晰,毕竟常用的就是 判断、循环、和几个格式化函数</h3> <h3>模板语法其实很简单,只需要:</h3> <p><strong>1: 记住模板定界符实际上只是将定界符内的内容 替换成了 &lt;?php echo 定界符内的内容;?&gt; </strong> <strong>2: 理解 使用html标签的属性-值来声明需要执行的php方法和参数,其它和php代码都一样 </strong></p> <p><strong>&lt;font color=red&gt;注意:&lt;/font&gt;需要在入口文件中加载 view 依赖库</strong> <strong>&lt;font color=red&gt;注意:&lt;/font&gt;DEBUG['level'] &gt; 1 的情况下才会&lt;font color=red&gt;自动更新&lt;/font&gt;模板的缓存文件,否则如果修改了模板文件,需要&lt;font color=red&gt;手动删除&lt;/font&gt;对应的模板缓存文件(/tmp/run/应用目录)</strong></p> <h1>控制器中使用视图模板</h1> <pre><code>&amp;lt;?php namespace app\ctrl; use nec\z\view; // 使用view视图类 class index { function index () { $user = [ 'username'=&amp;gt;'user', 'userid'=&amp;gt;123, ]; $data = [ 'head'=&amp;gt;'this is head', 'body'=&amp;gt;'this is body', ]; $login = false; view::Assign('user', $user); // 分配user变量 view::Assign('data', $data); // 分配data变量 view::Assign('login', $login); // 分配login变量 view::Display(); // 渲染默认路径下的模板 // view::Display('default/user/index'); // 渲染default样式目录下/user/index.html的模板 } }</code></pre> <h1>模板路径</h1> <p><strong>默认路径:当前风格目录/控制器名/操作名.html</strong></p> <p><strong>view::Display('index') 路径:当前风格目录/控制器名/index.html</strong></p> <p><strong>view::Display('user/index') 路径:当前风格目录/user/index.html</strong></p> <p><strong>view::Display('user/&lt;font color=red&gt;new_dir&lt;/font&gt;/header') 路径:当前风格目录/user/&lt;font color=red&gt;new_dir&lt;/font&gt;/header.html</strong></p> <p><strong>也可以使用绝对路径,可以配合常量使用</strong></p> <pre><code>$tpl = P_VIEW_MODULE . 'theme/user/index.html' // 当前模块模板目录/theme风格目录/user/index.html view::Display($tpl)</code></pre> <p><strong>其它模块目录</strong></p> <pre><code>$tpl = P_APP . 'test/view/' . THEME . '/user/index.html' // 当前应用目录/test模块/模板目录/当前风格目录/user/index.html view::Display($tpl)</code></pre> <h1>模板引用:</h1> <ul> <li><strong>模板内使用 import 标签来引入其它模板文件,file属性指定要引入的模板文件路径,name属性指定要引入文件中的哪个 template 的内容;不指定 name 的情况下将导入整个文件内容</strong></li> <li><strong>import 中可以使用&lt;font color=red&gt;路径常量&lt;/font&gt;,&lt;font color=red&gt;不需要&lt;/font&gt;添加变量解析标签</strong></li> <li><strong>支持 ../ 相对路径</strong></li> <li><strong>支持模板文件的嵌套引用</strong></li> </ul> <pre><code>&amp;lt;!DOCTYPE html&amp;gt; &amp;lt;head&amp;gt; &amp;lt;import file=&amp;quot;block/common.html&amp;quot; name=&amp;quot;head&amp;quot;/&amp;gt; &amp;lt;/head&amp;gt; &amp;lt;body&amp;gt; &amp;lt;import file=&amp;quot;block/common.html&amp;quot; name=&amp;quot;nav&amp;quot;/&amp;gt; &amp;lt;div class=&amp;quot;container&amp;quot;&amp;gt; &amp;lt;h1&amp;gt;&amp;lt;{ $title }&amp;gt;&amp;lt;/h1&amp;gt; &amp;lt;import file=&amp;quot;index/block/content.html&amp;quot; name=&amp;quot;content&amp;quot;/&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;import file=&amp;quot;block/common.html&amp;quot; name=&amp;quot;footer&amp;quot;/&amp;gt; &amp;lt;/body&amp;gt; &amp;lt;/html&amp;gt;</code></pre> <p><strong>要引用的模板文件:common.html</strong></p> <pre><code>&amp;lt;!-- head --&amp;gt; &amp;lt;template name=&amp;quot;head&amp;quot;&amp;gt; &amp;lt;meta name=&amp;quot;viewport&amp;quot; content=&amp;quot;width=device-width, initial-scale=1&amp;quot;&amp;gt; &amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;&amp;lt;{U_RES}&amp;gt;/css/main.css&amp;quot;&amp;gt; &amp;lt;/template&amp;gt; &amp;lt;!-- 导航 --&amp;gt; &amp;lt;template name=&amp;quot;nav&amp;quot;&amp;gt; &amp;lt;nav class=&amp;quot;navbar&amp;quot;&amp;gt; &amp;lt;/nav&amp;gt; &amp;lt;/template&amp;gt; &amp;lt;!-- 页脚 --&amp;gt; &amp;lt;template name=&amp;quot;footer&amp;quot;&amp;gt; &amp;lt;footer class=&amp;quot;footer&amp;quot;&amp;gt;&amp;lt;/footer&amp;gt; &amp;lt;/template&amp;gt;</code></pre> <h1>模板语法:</h1> <p><strong>&lt;font color=red&gt;注意:&lt;/font&gt;模板内的标签&lt;font color=red&gt;必须配对或者闭合&lt;/font&gt;,不能前一个 template 写 div 标签, 下一个 template 才写 /div 闭合,否则可能会解析错误!&lt;/font&gt;</strong></p> <h2>显示变量:&lt;{ $param }&gt;</h2> <pre><code>&amp;lt;!DOCTYPE html&amp;gt; &amp;lt;html&amp;gt; &amp;lt;head&amp;gt; &amp;lt;meta http-equiv=&amp;quot;Content-Type&amp;quot; content=&amp;quot;text/html;charset=UTF-8&amp;quot;&amp;gt; &amp;lt;/head&amp;gt; &amp;lt;body&amp;gt; &amp;lt;div&amp;gt;$param变量值是:&amp;lt;{ $param }&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;div&amp;gt;可以使用三元表达式:&amp;lt;{ $istrue ? $param : 'false' }&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;/body&amp;gt; &amp;lt;/html&amp;gt;</code></pre> <h2>模板标签</h2> <p><strong>框架采用模板标签的方式,兼顾简洁灵活</strong> <strong>规则同样很简单:将判断,循环等语句写在标签的属性中即可</strong> <strong>理解规则之后就可以很方便地使用了</strong></p> <p><strong>&lt;font color=red&gt;自定义标签名&lt;/font&gt;:默认使用&lt;php&gt;&lt;/php&gt;标签,如有需要,可以修改配置文件的 VIEW['php_tag'] 字段</strong></p> <ul> <li> <p><strong>条件判断</strong></p> <pre><code>&amp;lt;php if=&amp;quot;$param1&amp;quot;&amp;gt; &amp;lt;p&amp;gt;变量$param1是真&amp;lt;/p&amp;gt; &amp;lt;/php&amp;gt; &amp;lt;php elseif=&amp;quot;$param2&amp;quot;&amp;gt; &amp;lt;p&amp;gt;变量$param2是真&amp;lt;/p&amp;gt; &amp;lt;/php&amp;gt; &amp;lt;php else&amp;gt; &amp;lt;p&amp;gt;变量$param1和$param2都是假&amp;lt;/p&amp;gt; &amp;lt;/php&amp;gt;</code></pre> </li> <li> <p><strong>foreach 循环</strong> <strong>此处直接写原生语句给&lt;font color=red&gt;$class&lt;/font&gt;变量赋值</strong></p> <pre><code>&amp;lt;php foreach=&amp;quot;$navs as $key=&amp;gt;$value&amp;quot;&amp;gt; &amp;lt;?php $class = $key === $active ? 'active' : 'nav-item'; ?&amp;gt; &amp;lt;a class=&amp;quot;&amp;lt;{ $class }&amp;gt;&amp;quot; href=&amp;quot;&amp;lt;{ $key }&amp;gt;&amp;quot;&amp;gt;&amp;lt;{ $key }&amp;gt;&amp;lt;/a&amp;gt; &amp;lt;/php&amp;gt;</code></pre> </li> <li> <p><strong>for 循环</strong></p> <pre><code>&amp;lt;php for=&amp;quot;$i=0; $i !== count($param); ++$i&amp;quot;&amp;gt; &amp;lt;h2 style=&amp;quot;color:red;&amp;quot;&amp;gt;&amp;lt;{ $param[$i] }&amp;gt;&amp;lt;/h2&amp;gt; &amp;lt;/php&amp;gt;</code></pre> </li> <li> <p><strong>while 循环</strong></p> <pre><code>&amp;lt;php while=&amp;quot;--$i&amp;quot;&amp;gt; &amp;lt;h3 style=&amp;quot;color:cyan;&amp;quot;&amp;gt;&amp;lt;{ $i }&amp;gt;&amp;lt;/h3&amp;gt; &amp;lt;/php&amp;gt;</code></pre> </li> <li><strong>switch 语句</strong> <pre><code>&amp;lt;php switch=&amp;quot;$param&amp;quot;&amp;gt; &amp;lt;php case=&amp;quot;0&amp;quot; break&amp;gt;&amp;lt;p&amp;gt;000000000&amp;lt;/p&amp;gt;&amp;lt;/php&amp;gt; &amp;lt;php case=&amp;quot;1&amp;quot;&amp;gt;&amp;lt;p&amp;gt;11111111&amp;lt;/p&amp;gt;&amp;lt;/php&amp;gt; &amp;lt;php case=&amp;quot;2&amp;quot; break&amp;gt;&amp;lt;p&amp;gt;2222222222&amp;lt;/p&amp;gt;&amp;lt;/php&amp;gt; &amp;lt;php default&amp;gt;&amp;lt;h1&amp;gt;default&amp;lt;/h1&amp;gt;&amp;lt;/php&amp;gt; &amp;lt;/php&amp;gt;</code></pre></li> </ul> <h1>模板的复用</h1> <ul> <li><strong>定义一个模板 hello:</strong> <strong>其中&lt;font color=red&gt; 模板中使用的变量 &lt;/font&gt;可以通过&lt;font color=red&gt; import &lt;/font&gt;标签绑定变量</strong> <strong>变量名必须&lt;font color=red&gt; 小写 &lt;/font&gt;,由于变量的&lt;font color=red&gt; 作用域 &lt;/font&gt;在整个视图中有效,建议添加前缀区别</strong> <strong>为方便阅读和引用, 需在&lt;font color=red&gt;被调用的模板中声明需要传入的参数名&lt;/font&gt; params=&quot;$var1,$var2&quot;</strong> <pre><code>&amp;lt;template name=&amp;quot;hello&amp;quot; params=&amp;quot;$tpl_cfg,$tpl_arr&amp;quot;&amp;gt; &amp;lt;p&amp;gt;a: &amp;lt;{$tpl_cfg['a']}&amp;gt;&amp;lt;/p&amp;gt; &amp;lt;p&amp;gt;b: &amp;lt;{$tpl_cfg['b']}&amp;gt;&amp;lt;/p&amp;gt; &amp;lt;php foreach=&amp;quot;$tpl_arr['arr'] as $k=&amp;gt;$v&amp;quot;&amp;gt; &amp;lt;p&amp;gt;&amp;lt;span&amp;gt;&amp;lt;{$k}&amp;gt;&amp;lt;/span&amp;gt;:&amp;lt;span&amp;gt;&amp;lt;{$v}&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt; &amp;lt;/php&amp;gt; &amp;lt;/template&amp;gt;</code></pre></li> <li><strong>引用多个模板 hello:</strong> <strong>通过&lt;font color=red&gt; :tpl_cfg&lt;/font&gt; 和 &lt;font color=red&gt;:tpl_arr &lt;/font&gt; 属性绑定到模板内的&lt;font color=red&gt; $tpl_cfg &lt;/font&gt;和&lt;font color=red&gt; $tpl_arr &lt;/font&gt;变量</strong> <strong>&lt;font color=red&gt; :tpl_cfg&lt;/font&gt; 和 &lt;font color=red&gt;:tpl_arr &lt;/font&gt;属性的值可以是变量名 &lt;font color=red&gt;:tpl_cfg=&quot;$cfg&quot; :tpl_arr=&quot;$arr&quot;&lt;/font&gt; 也可以直接赋值 &lt;font color=red&gt;:tpl_arr=&quot;['a'=&gt;'111', 'b'=&gt;'222']&quot;&lt;/font&gt;</strong> <pre><code>&amp;lt;div&amp;gt; &amp;lt;import file=&amp;quot;common.html&amp;quot; name=&amp;quot;hello&amp;quot; :tpl_cfg=&amp;quot;['a'=&amp;gt;'111', 'b'=&amp;gt;'222']&amp;quot; :tpl_arr=&amp;quot;$arr&amp;quot; /&amp;gt; &amp;lt;hr&amp;gt; &amp;lt;import file=&amp;quot;common.html&amp;quot; name=&amp;quot;hello&amp;quot; :tpl_cfg=&amp;quot;$cfg1&amp;quot; :tpl_arr=&amp;quot;$arr1&amp;quot; /&amp;gt; &amp;lt;hr&amp;gt; &amp;lt;import file=&amp;quot;common.html&amp;quot; name=&amp;quot;hello&amp;quot; :tpl_cfg=&amp;quot;$cfg2&amp;quot; :tpl_arr=&amp;quot;$arr2&amp;quot; /&amp;gt; &amp;lt;/div&amp;gt;</code></pre></li> <li><strong>控制器内分配变量:</strong> <pre><code>$arr = [1,2,3]; $arr1 = [11,22,33]; $arr2 = [111,222,333]; $cfg1 = ['a'=&amp;gt;'a', 'b'=&amp;gt;'b']; $cfg2 = ['a'=&amp;gt;'aa', 'b'=&amp;gt;'bb']; view::Assign('arr', $arr); view::Assign('arr1', $arr1); view::Assign('arr2', $arr2); view::Assign('cfg1', $cfg1); view::Assign('cfg2', $cfg2); view::display();</code></pre></li> </ul> <h1>自定义标签</h1> <ul> <li><strong>配置文件增加 custom_tags 字段:</strong> <pre><code>'VIEW' =&amp;gt; [ 'custom_tags'=&amp;gt;[ 'set'=&amp;gt;['model\testc', 'testb', '$var'], // [类, 方法(默认为fn), 接收变量名(无此参数将重写此标签(将返回值当作php代码写入模板)] 'loop'=&amp;gt;['model\viewTag', 'loop'], 'if' =&amp;gt; ['model\viewTag', 'if'], 'data'=&amp;gt;['', 'testb', '$var'], // 参数1留空则表示调用全局函数 ], ]</code></pre></li> <li><strong>模板中使用自定义标签</strong> $var 是上面配置文件中定义的接收数据的变量名 标签各属性都被作为参数传入函数 <code>a=&amp;quot;1&amp;quot; src=&amp;quot;abcd&amp;quot;</code> <strong>示例:该标签实际执行的是 $var = model\testc::testb($attrs); 使用 $var 接收返回值</strong> <strong>标签属性 :var &lt;font color=red&gt;重新指定了接收变量为 $asd&lt;/font&gt;</strong> <pre><code>&amp;lt;set :var=&amp;quot;$asd&amp;quot; :num=&amp;quot;5&amp;quot; /&amp;gt; &amp;lt;p style=&amp;quot;color: red;&amp;quot;&amp;gt;&amp;lt;{ json_encode($asd) }&amp;gt;&amp;lt;/p&amp;gt;</code></pre> <p><strong>示例:当方法返回值是数组的时候可以指定多个接收变量名 (用 , 分隔) 注意顺序要和返回值一致</strong></p> <pre><code>&amp;lt;set :num=&amp;quot;5&amp;quot;/&amp;gt; &amp;lt;p style=&amp;quot;color: red;&amp;quot;&amp;gt;&amp;lt;{ $var1 }&amp;gt;&amp;lt;/p&amp;gt; &amp;lt;p style=&amp;quot;color: red;&amp;quot;&amp;gt;&amp;lt;{ $var2 }&amp;gt;&amp;lt;/p&amp;gt; &amp;lt;p style=&amp;quot;color: red;&amp;quot;&amp;gt;&amp;lt;{ $var3 }&amp;gt;&amp;lt;/p&amp;gt;</code></pre></li> </ul> <h2>重写标签的返回代码</h2> <p><strong>&lt;font color=red&gt;详见示例: /model/viewTag.php&lt;/font&gt;</strong></p> <h1>缓存页面 GetCache()</h1> <p><strong>&lt;font color=red&gt;只适合不需要登录的公共页面&lt;/font&gt;,需要登录的页面,因为每个用户的页面是有区别的,所以不适合缓存页面</strong></p> <table> <thead> <tr> <th>参数</th> <th>默认值</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>无</td> <td>无 缓存时间</td> </tr> <tr> <td>2</td> <td>同 Display()</td> <td>模板文件</td> </tr> <tr> <td>3</td> <td>0</td> <td>限制url参数,可以是:&lt;br&gt;&lt;font color=red&gt;假:&lt;/font&gt;无视url参数,只生成同一个页面。&lt;br&gt;&lt;font color=red&gt;真:&lt;/font&gt;url参数不同生成不同的缓存页面。&lt;font color=red&gt;数组:&lt;/font&gt;过滤不在数组中的url参数。</td> </tr> </tbody> </table> <p><strong>参数3 通常用来做列表页的缓存,根据分页参数不同生成相应的缓存页面</strong> <strong>&lt;font color=red&gt;最好&lt;/font&gt;限制一下有效参数,&lt;font color=red&gt;并且&lt;/font&gt;事先判断一下取值范围</strong> <strong>因为搜索引擎,蜘蛛,爬虫 == 可能会尝试不同的url参数抓取页面,会造成无故生成大量的相同内容的缓存页面</strong> <strong>&lt;font color=red&gt;只要&lt;/font&gt;调用了GetCache()方法,后面的Display()就会生成缓存页面,&lt;font color=red&gt;而且&lt;/font&gt;Display()中指定的模板文件参数&lt;font color=red&gt;无效&lt;/font&gt;,使用的是GetCache()中的模板文件</strong></p> <pre><code>public static function index() { $html = view::GetCache(60, 'index.html'); // 无视url参数 $html = view::GetCache(60, 'index.html', ['p']); // p 参数不同生成不同的缓存页面 $html = view::GetCache(60, 'index.html', ['p'=&amp;gt;1]); // 指定p参数 $html = view::GetCache(60, 'index.html', true); // url参数不同生成不同的缓存页面 if($html){ echo $html; } else { // ... 代码 view::Display(); } }</code></pre> <h1>压缩编译生成的模板页</h1> <p><strong>配置文件 VIEW 的 compress 字段</strong> <strong>可选值:</strong></p> <ul> <li>1:只删除注释</li> <li>2:删除注释并压缩</li> </ul> <p><strong>&lt;font color=red&gt;注意:&lt;/font&gt;上面的配置对 javascript 和 css 同样生效</strong> <strong>&lt;font color=red&gt;如果有不同策略:&lt;/font&gt;请配置为数组格式:</strong> <strong>[0]:html 的参数值,[1]:style标签内css的参数值,[2]:javascript 的参数值</strong> <strong>例如:[2, 1, 1] 表示 html:删除注释并压缩,css:只删除注释,javascript:只删除注释</strong></p>

页面列表

ITEM_HTML