Html模板引擎

    默认分隔符为

    标签说明

    直接输出变量

    //入库转译只需一次,输出的时候转译要n次。所以这边输出不做htmlspecialchars转译。建议入库前转义。

    {{$a}}

    {{+$a}} htmlspecialchars转译输出 v2.7.1+可用

    输出数组

    • 一维数组: {{$a['key']}}或者{{$a.key}]} {{+$a['key']}}或者{{+$a.key}]} htmlspecialchars转译输出 v2.7.1+可用

    • 二维数组: {{$a['key1']['key2']}}或者 {{$a.key1.key2}} {{+$a['key1']['key2']}}或者 {{+$a.key1.key2}} htmlspecialchars转译输出 v2.7.1+可用

    • 多维数组: {{echo $a[..]….}} 用echo输出{{+echo $a[..]….}} htmlspecialchars转译输出 v2.7.1+可用

    判断语句

    1. {{if $abc <1 }}
    2. 。。。。
    3. {{elseif}} 【可选】
    4. 。。。。
    5. {{else}} 【可选】
    6. 。。。。
    7. {{/if}}

    循环语句

    loop

    1. {{loop $abc $key $val}} 【$key可选】
    2. 。。。
    3. {{/loop}}
    1. {{foreach $abc $key $val}} 【$key可选】
    2. 。。。
    3. {{/foreach}}

    包含公共模板

    1. // 包含项目目录`/Views/Site/Index/index.html`模板文件

    执行简单php代码

    1. {{eval ...}}

    执行某些函数

    1. {{echo trim(' abc ')}}

    格式化日期时间

    设置插件挂载点

    1. {{hook test}}

    获取get、post、request请求参数

    1. {{get name}}、{{post name}}、{{request name}}

    获取配置文件配置项

    1. {{config key1.key2}}

    获取语言包配置项

    1. {{lang key1.key2}}

    根据url_model配置项,生成相应的url(如果是在子目录自动处理)

    1. {{url 'Index/index'}}
    2. 生成的地址可能是:
    3. /index.php/Index/index
    4. /Index/index.html
    5. /index.php?r=/Index/index
    6. subdir/Index/index.html
    7. subdir/index.php/Index/index
    8. ...

    生成表单令牌

    1. {{token}}

    输出静态文件目录基地址

    1. {{public}}
    2. 生成的地址如:
    3. http://baidu.com/index.php 生成 /
    4. http://baidu.com/public/index.php 生成/public/
    5. http://baidu.com/subdir/public/ 生成/subdir/public/
    6. 修改配置`static__path` => 'http://img.attach.com/' 生成http://img.attach.com/

    生成指静态文件目录的路径(自动处理项目部署在二级三级目录的问题),如果是存放在cdn/单独服务器有独立域名 ,只要修改配置文件的static__path配置项为相应地址即可

    静态文件管理

    模板中判断有无某个模块的权限

    使用模板标签{{acl xxx}}

    1. {{acl user/add}}
    2. 我有添加用户的权限
    3. {{/acl}}
    4. {{acl user/add}}
    5. 我有添加用户的权限
    6. {{else}}
    7. {{/acl}}

    模板布局

    模板布局文件都是声明在应用名/View/layout下,如声明一个模板布局文件 xxx/View/layout/master.html 内容为:

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" />
    5. <title>标题</title>
    6. <link href="{{public}}css/Test/style.css" rel="stylesheet" type="text/css" media="screen" />
    7. </head>
    8. <body>
    9. {{block content}}
    10. aaa
    11. {{/block}}
    12. </body>
    13. </html>

    {{urldeper}}标签

    模板注释

    1. {{comment 我只是一个模板注释}}

    代码注释,编译后会自动删除

    自定义一个模板标签.

    1. /**
    2. * 添加一个模板标签
    3. *
    4. * @param string $pattern 正则
    5. * @param string $replacement 替换成xx内容
    6. * @param bool $haveDelimiter $pattern的内容是否要带上左右定界符
    7. *
    8. * @return bool
    9. */
    10. public function addRule($pattern, $replacement, $haveDelimiter = true)
    11. {
    12. if ($pattern && $replacement) {
    13. $this->pattern = $haveDelimiter ? '#'.$this->options['leftDelimiter'].$pattern.$this->options['rightDelimiter'].'#s' : "#{$pattern}#s";
    14. $this->replacement = $replacement;
    15. return true;
    16. }
    17. }