视图模板
<h2><font color=red>注意:</font>DEBUG['level'] > 1 的情况下才会<font color=red>自动更新</font>模板的缓存文件,否则如果修改了模板文件,需要<font color=red>手动删除</font>对应的模板缓存文件(/tmp/run/应用目录)</h2>
<h2><font color=red>注意:</font>如果你在视图中使用VUE,不要使用 @click 等带有特殊字符的属性,可以改用 v-on:click。</h2>
<h1>控制器中使用视图模板</h1>
<pre><code><?php
namespace ctrl;
use z\view; // 使用view视图类
class index {
function index ()
{
$user = [
'username'=>'user',
'userid'=>123,
];
$data = [
'head'=>'this is head',
'body'=>'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/<font color=red>new_dir</font>/header') 路径:当前风格目录/user/<font color=red>new_dir</font>/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_VER . 'test/view/' . THEME . '/user/index.html'
// 当前应用目录/test模块/模板目录/当前风格目录/user/index.html
view::Display($tpl)</code></pre>
<h1>模板文件的后缀名</h1>
<p><strong>配置文件中的 VIEW['ext'] 配置后缀名</strong></p>
<h1>模板引用:</h1>
<ul>
<li><strong>模板内使用 import 标签来引入其它模板文件,file属性指定要引入的模板文件路径,name属性指定要引入文件中的哪个 template 的内容</strong></li>
<li><strong>import 中可以使用<font color=red>路径常量</font>,<font color=red>不需要</font>添加变量解析标签</strong></li>
<li><strong>支持模板文件的嵌套引用</strong></li>
</ul>
<pre><code><!DOCTYPE html>
<head>
<import file="block/common.html" name="head"/>
</head>
<body>
<import file="block/common.html" name="nav"/>
<div class="container">
<h1><{ $title }></h1>
<import file="index/block/content.html" name="content"/>
</div>
<import file="block/common.html" name="footer"/>
</body>
</html></code></pre>
<p><strong>要引用的模板文件:common.html</strong></p>
<pre><code><!-- head -->
<template name="head">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="<{U_RES_VER}>/css/main.css">
</template>
<!-- 导航 -->
<template name="nav">
<nav class="navbar">
</nav>
</template>
<!-- 页脚 -->
<template name="footer">
<footer class="footer"></footer>
</template></code></pre>
<h1>模板语法:</h1>
<h3><font color=red>注意:</font>模板内的标签<font color=red>必须配对或者闭合</font>,不能前一个 template 写 div 标签, 下一个 template 才写 /div 闭合,否则可能会解析错误!</font></h3>
<h3><font color=red>注意:</font>模板内<font color=red>根节点</font>不支持<?php ...代码; ?>原生标签,如果必须要用,请使用<php>...代码;</php>代替</h3>
<h2>显示变量:<{ $param }></h2>
<pre><code><!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>
<div>$param变量值是:<{ $param }></div>
<div>可以使用三元表达式:<{ $istrue ? $param : 'false' }></div>
</body>
</html></code></pre>
<h2>模板标签</h2>
<p><strong>框架采用模板标签的方式,兼顾简洁灵活</strong>
<strong>规则同样很简单:将判断,循环等语句写在标签的属性中即可</strong>
<strong>理解规则之后就可以很方便地使用了</strong></p>
<p><strong><font color=red>自定义标签名</font>:默认使用<php></php>标签,如有需要,可以修改配置文件的 VIEW['php_tag'] 字段</strong></p>
<ul>
<li>
<p><strong>条件判断</strong></p>
<pre><code><php if="$param1">
<p>变量$param1是真</p>
</php>
<php elseif="$param2">
<p>变量$param2是真</p>
</php>
<php else>
<p>变量$param1和$param2都是假</p>
</php></code></pre>
</li>
<li>
<p><strong>foreach 循环</strong>
<strong>此处直接写原生语句给<font color=red>$class</font>变量赋值</strong></p>
<pre><code><php foreach="$navs as $key=>$value">
<?php $class = $key === $active ? 'active' : 'nav-item'; ?>
<a class="<{ $class }>" href="<{ $key }>"><{ $key }></a>
</php></code></pre>
</li>
<li>
<p><strong>for 循环</strong></p>
<pre><code><php for="$i=0; $i !== count($param); ++$i">
<h2 style="color:red;"><{ $param[$i] }></h2>
</php></code></pre>
</li>
<li>
<p><strong>while 循环</strong></p>
<pre><code><php while="--$i">
<h3 style="color:cyan;"><{ $i }></h3>
</php></code></pre>
</li>
<li><strong>switch 语句</strong>
<pre><code><php switch="$param">
<php case="0" break><p>000000000</p></php>
<php case="1"><p>11111111</p></php>
<php case="2" break><p>2222222222</p></php>
<php default><h1>default</h1></php>
</php></code></pre></li>
</ul>
<h1>模板的复用(4.1.0)</h1>
<ul>
<li><strong>定义一个模板 hello:</strong>
<strong>其中<font color=red> 模板中使用的变量 </font>可以通过<font color=red> import </font>标签绑定变量</strong>
<strong>变量名必须<font color=red> 小写 </font>,由于变量的<font color=red> 作用域 </font>在整个视图中有效,建议添加前缀区别</strong>
<pre><code><template name="hello">
<p>a: <{$tpl_cfg['a']}></p>
<p>b: <{$tpl_cfg['b']}></p>
<php foreach="$tpl_arr['arr'] as $k=>$v">
<p><span><{$k}></span>:<span><{$v}></span></p>
</php>
</template></code></pre></li>
<li><strong>引用多个模板 hello:</strong>
<strong>通过<font color=red> :tpl_cfg</font> 和 <font color=red>:tpl_arr </font> 属性绑定到模板内的<font color=red> $tpl_cfg </font>和<font color=red> $tpl_arr </font>变量</strong>
<strong><font color=red> :tpl_cfg</font> 和 <font color=red>:tpl_arr </font>属性的值可以是变量名 <font color=red>:tpl_cfg="$cfg" :tpl_arr="$arr"</font>
也可以直接赋值 <font color=red>:tpl_arr="['a'=>'111', 'b'=>'222']"</font></strong>
<pre><code><div>
<import file="common.html" name="hello" :tpl_cfg="['a'=>'111', 'b'=>'222']" :tpl_arr="$arr" />
<hr>
<import file="common.html" name="hello" :tpl_cfg="$cfg1" :tpl_arr="$arr1" />
<hr>
<import file="common.html" name="hello" :tpl_cfg="$cfg2" :tpl_arr="$arr2" />
</div></code></pre></li>
<li><strong>控制器内分配变量:</strong>
<pre><code>$arr = [1,2,3];
$arr1 = [11,22,33];
$arr2 = [111,222,333];
$cfg1 = ['a'=>'a', 'b'=>'b'];
$cfg2 = ['a'=>'aa', 'b'=>'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>自定义标签(4.2.0)</h1>
<ul>
<li><strong>配置文件增加 custom_tags 字段:</strong>
<pre><code>'VIEW' => [
'custom_tags'=>[
// 标签名 => [类库路径(类命名空间+类名), 函数名, 接收数据的变量名]
'data-size' => ['app\common\tags', 'size', '$var'], //示例:使用类库中的方法(注意必须是静态方法)
'data-any' => ['', 'size', '$var'], //示例:使用全局函数(类库路径置空)
'data' => ['app\common\tags', '', '$var'], //示例:在模板中指定方法名
],
]</code></pre></li>
<li><strong>模板中使用自定义标签</strong>
$var 是上面配置文件中定义的接收数据的变量名
标签属性 args 是需要传入函数的参数
如果是字符串参数需要使用 '' 包裹 <code>args="'abc'"</code>
多个参数用 , 分隔 <code>args="'abc', 32"</code>
可以使用变量名或常量名做参数:<code>args="$abc, TIME"</code>
<strong>示例:该标签实际执行的是 app\common\tags::size(args); 使用配置中的默认接收变量名 $var 接收返回值</strong>
<pre><code><data-size args="12345678" />
<p style="color: red;"><{ $var }></p></code></pre>
<p><strong>示例:使用全局函数(类库路径置空)</strong></p>
<pre><code><data-any args="12345678, 3" />
<p style="color: red;"><{ $var }></p></code></pre>
<p><strong>示例:在模板中指定方法名为 size,指定接收变量名为 $aa</strong></p>
<pre><code><data action="size" var="$aa" args="12345678, 3" />
<p style="color: red;"><{ $aa }></p></code></pre>
<p><strong>示例:当方法返回值是数组的时候可以指定多个接收变量名 (用 , 分隔) 注意顺序要和返回值一致</strong></p>
<pre><code><data action="size" var="$aa, $bb" args="12345678, 3" />
<p style="color: red;"><{ $aa }></p>
<p style="color: red;"><{ $bb }></p></code></pre></li>
</ul>
<h1>缓存页面 GetCache()</h1>
<p><strong><font color=red>只适合不需要登录的公共页面</font>,需要登录的页面,因为每个用户的页面是有区别的,所以不适合缓存页面</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参数,可以是:<br><font color=red>假:</font>无视url参数,只生成同一个页面。<br><font color=red>真:</font>url参数不同生成不同的缓存页面。<font color=red>数组:</font>过滤不在数组中的url参数。</td>
</tr>
</tbody>
</table>
<p><strong>参数3 通常用来做列表页的缓存,根据分页参数不同生成相应的缓存页面</strong>
<strong><font color=red>最好</font>限制一下有效参数,<font color=red>并且</font>事先判断一下取值范围</strong>
<strong>因为搜索引擎,蜘蛛,爬虫 == 可能会尝试不同的url参数抓取页面,会造成无故生成大量的相同内容的缓存页面</strong>
<strong><font color=red>只要</font>调用了GetCache()方法,后面的Display()就会生成缓存页面,<font color=red>而且</font>Display()中指定的模板文件参数<font color=red>无效</font>,使用的是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'=>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><font color=red>注意:</font>上面的配置对 javascript 和 css 同样生效</strong>
<strong><font color=red>如果有不同策略:</font>请配置为数组格式:</strong>
<strong>[0]:html 的参数值,[1]:style标签内css的参数值,[2]:javascript 的参数值</strong>
<strong>例如:[2, 1, 1] 表示 html:删除注释并压缩,css:只删除注释,javascript:只删除注释</strong></p>