使用函数

  1. 控制器端先赋值一个密码的变量,模版区设置 md5 加密操作;
    1. $this->assign('password', '123456');
    2. {$password|md5}
  2. 系统默认在编译的会采用 htmlentities 过滤函数防止 XSS 跨站脚本攻击;
    3. 如果你想更换一个过滤函数,比如 htmlspecialchars,可以在配置文件设置;
    4. 具体在 config 下的 template.php 中,增加一条如下配置即可;
    1. 'default_filter' => 'htmlspecialchars'
  3. 如果在某个字符,你不需要进行 HTML 实体转义的话,可以单独使用 raw 处理;
    1. {$user['email']|raw}
  4. 系统还提供了一些固定的过滤方法,如下:
    image.png
    1. $this->assign('time', time());
    2. {$time|date='Y-m-d'}
    3. $this->assign('number', '14');
    4. {$number|format='%x'}
  5. 如果函数中,需要多个参数调用,直接用逗号隔开即可;
    1. {$name|substr=0,3}
  6. 在模版中也支持多个函数进行操作,用|号隔开即可,函数从左到右依次执行;
    1. {$password|md5|upper|substr=0,3}
  7. 你也可以在模版中直接使用 PHP 的语法模式,该方法不会使用过滤转义:
    1. {:substr(strtoupper(md5($password)), 0, 3)}

    运算符

  8. 在模版中的运算符有+、-、*、/、%、++、—等;
    1. {$number + $number}
  9. 如果模版中有运算符,则函数方法则不再支持;
    1. {$number + $number|default='没有值'}
  10. 模版也可以实现三元运算,包括其它写法;
    1. {$name ? '正确' : '错误'}
    2. //$name 为 true 返回正确,否则返回错误
    3. {$name ?= '真'}
    4. //$name 为 true 返回真
    5. {$Think.get.name ?? '不存在'}
    6. //??用于系统变量,没有值时输出
    7. {$name ?: '不存在'}
    8. //?:用于普通变量,没有值时输出
  11. 三元运算符也支持运算后返回布尔值判断;
    1. {$a == $b ? '真' : '假'}