ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
##**模版引擎** **(以下第一个---->理解为变量未缓存编译结果,第二个为缓存后结果)** **(配置以默认为准)** ###*模版配置* * * * * * ~~~ $template_config = [ // 模板引擎标签开始标记 'left_delim' => '<{', // 模板引擎标签结束标记 'right_delim' => '}>', ]; ~~~ ###*方法* * * * * * ~~~ /** * 模版编译 * * @param string $content 模版内容 * @param array $data 模版变量 * [ * key=>['cache'=>bool,'value'=>miexd] * ,... * ] * @param array $language 语言变量 * [ * key=>'value' * ,.... * ] * * @return string */ \msqphp\core\template\Template::commpile(string $content, array $data = [], array $language = []) : string ~~~ ###*注意事项* * * * * * + 左标签后禁止带有空格,左标签最好正常闭合,不然会有未知问题 > <{ a错误 > <{$a } 错误,未闭合 > <{a}> 正确 + 右标签前可有空格 > a }>允许 + 如果可输入空格,则一般支持多个空格 + foreach等可以省略部分空格,但不推荐 > <{foreach $array as $key=>$val}> > <{if $a===$b}> + 推荐左标签: <{ , 右: }> ,暂时不与html或css或js冲突 + 数据缓存为真实缓存 >相当于将所有指定缓存数据所参与的所有语句(if,foreach等等)进行预编译 >使得编译后内容展示的时候不需要缓存数据(半成品) + 支持标签嵌套,但不推荐 ###*变量赋值:* * * * * * ~~~ $a = 'value'; //模版内容 ----> 变量不缓存 ----> 变量缓存 <{$a}> ----> <?php echo $a;?> ----> value ~~~ ###*数组赋值:* * * * * * 支持php数组写法和以.分割两种写法,无限维数 ~~~ $a = ['name'=>'user','pass'=>test',0=>'a']; <{$a.name}> ----> <?php echo $a['name'];?> ----> user <{$a['name']}> ----> <?php echo $a['name'];?> ----> user <{$a.0}> ----> <?php echo $a[0];?> ----> a <{$a[0]}> ----> <?php echo $a[0];?> ----> a ~~~ ###*引入文件* * * * * * 引入文件后按模版引擎解析,文件不存在报错 ~~~ <{include file_path}> /** * @example */ <{include ......../resources/templates/home/user/top.tpl}> ~~~ ###*语言* * * * * * **不存在抛出异常** **解析时不分语言,在传参时区分** ~~~ $lang = [ 'username' => '用户名' ]; $lang_english = [ 'username' => 'username' ]; <{language.username}> = <{lang.username}> 中----> '用户名' 英----> username ~~~ ###*常量* * * * * * **不存在抛出异常** **直接替换为对应值** ~~~ <{cont.usernam}> <{constant.usernam}> ~~~ ###*foreach:* * * * * * 支持写法: ~~~ <{foreach $array as $key => $value}> <{foreach $array as $value}> <{endforeach}> <{/endforeach}> ~~~ 例: ~~~ $arr = [1,2,3,4,5,6,7,8]; <{foreach $arr as $k => $v}> <{$k}>:<{$v}>. <{endforeach}> ----> <?php foreach($arr as $v) :?> <?php echo $k;?>:<?php echo $v;?>. <?php endforeach;?> ----> 0:11:22:33:44:55:66:77:8 ~~~ ###*if* * * * * * 支持写法 ~~~ //比较符支持:===,!==,==,!=,>,<,>=,<=,<>, //变量比较固定值,支持数字,字符串,布尔 <{if $a == 'string'}> <{if $b === 0}> //两个变量比较 <{if $a === $b}> <{elseif ....同上}> <{else}> <{endif}> <{/endif}> ~~~ 例: ~~~ $a = true; <{if $a === true}> 成功 <{endif}> ----> <?php if($a===true) : ?> 成功 <?php endif;?> ----> 成功 ~~~ ###*例* ~~~ <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1.0" /> <title></title> <link href="<{constant.CSS}>reset.css" rel="stylesheet" type="text/css" /> <link href="<{constant.CSS}>style.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="<{constant.JS}>jquery-2.1.1.min.js"></script> </head> <body> <form class="login_form" method="post" action="user/login"> <label class="msq-icon" for="username">&#xf007; </label><input type="text" name="username" placeholder="<{lang.username_info}>" required="required" autofocus="autofocus" /> <label class="msq-icon" for="password">&#xf023; </label><input type="password" name="password" placeholder="<{lang.password_info}>" required="required" minlength="6" maxlength="16" /> <input type="checkbox" name="auto_login" checked="checked " alt="<{lang.auto_login}>" title="<{lang.auto_login}>"/> <label for="auto_login" alt="<{lang.auto_login}>" title="<{lang.auto_login}>"><{lang.auto_login}></label> <input type="submit" value="<{lang.login}>"/> </form> </body> </html> //编译后---------------------------------------------------------- <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1.0" /> <title></title> <link href="www.example.com/css/reset.css" rel="stylesheet" type="text/css" /> <link href="www.example.com/css/style.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="www.example.com/js/jquery-2.1.1.min.js"></script> </head> <body> <form class="login_form" method="post" action="user/login"> <label class="msq-icon" for="username">&#xf007; </label><input type="text" name="username" placeholder="邮箱/手机/用户名" required="required" autofocus="autofocus" /> <label class="msq-icon" for="password">&#xf023; </label><input type="password" name="password" placeholder="请输入密码" required="required" minlength="6" maxlength="16" /> <input type="checkbox" name="auto_login" checked="checked " alt="下次自动登录" title="下次自动登录"/> <label for="auto_login" alt="下次自动登录" title="下次自动登录">下次自动登录</label> <input type="submit" value="登录"/> </form> </body> </html> ~~~