CY-Left

PHP开发语言

ThinkPHP 模板引擎使用-常量、变量使用及处理

【壹】 变量和变量处理

    <h2>一、函数处理</h2>
        <h3>字符加密:{$email} ->{$email|md5|substr=###,3,5}</h3>
        <h3>字符截取:{$email} ->{$email|substr=###,3,5}</h3>
        <h3>时间格式:{$time} ->{$time|date="Y-m-d", ###}</h3>
        <h3>no define val ->{$hello|default="default"}</h3>
    <h2>二、数学处理</h2>
        <h3>a + b = {$a+$b}</h3>
        <h3>a % b = {$a%$b}</h3>
        <h3>++a = {++$a}</h3>
        <h3>a++ = {$a++}</h3>
        {eq name='a' value='4'}

        <h3>相等</h3>
        {else/}
        <h3>不相等</h3>
        {/eq}
        {neq name='a' value='4'}
        <h3>不相等</h3>
        {else/}
        <h3>相等</h3>
        {/neq}

        {gt name='a' value='4'}
        <h3>大于</h3>
        {else/}
        <h3>不大于</h3>
        {/gt}
        {lt name='a' value='4'}
        <h3>小于</h3>
        {else/}
        <h3>不小于</h3>
        {/lt}
        {elt name='a' value='1'}
        <h3>不是小于等于</h3>
        {else/}
        <h3>是小于等于</h3>
        {/elt}

    <h2>三、系统常量</h2>
    <!--如下配置:'tpl_replace_string'  =>  ['__JS__' => '/static/javascript']-->
    <h3>__CY__</h3>
    <h3>__URL__</h3>
    <h3>__CSS__</h3>
    <h3>__JS__</h3>
    <h3>__ROOT__</h3>

    <h2>四、系统变量</h2>
    <h3>{$Think.server.HTTP_HOST}</h3>
    <h3>{$Think.env.PHP_STATUS}</h3>
    <h3>{$Think.session.email}</h3>
    <h3>{$Think.cookie.email}</h3>

    <h3>{$Think.get.id}</h3>
    <h3>{$Think.request.id}</h3>
    <h3>{$Think.post.id}</h3>

    <h2>五、注释</h2>
    {literal}
    {$email}
    {/literal}
    {/* 高级注释 */} <?php echo "高级注释"; // 高级注释,HTML 查看源也无法解析! ?>

【贰】 循环

<h1>【贰】 循环</h1>
    <h2>一、volist 循环</h2>
        <h3>普通循环</h3>
        {volist name="list" id="vo"}
            <h3>{$vo.name}</h3>
        {/volist}
        <br>
        <h3>1. 设置起始节点的循环</h3>
        {volist name="list" id="vo" offset='2'}
            <h3>{$vo.name}</h3>
        {/volist}
        <br>
        <h3>2. 带长度限制的循环</h3>
        {volist name="list" id="vo" length="1"}
            <h3>{$vo.name}</h3>
        {/volist}
        <br>
        <h3>3. 取余操作的循环</h3>
        {volist name="list" id="vo" mod="2"}
            <h3>{$vo.name}</h3>
        {/volist}
        <br>
            <h3>4. 判断为空的循环</h3>
        {volist name="list" id="vo" empty="list 为空时,这里将被打印,empty 可以在接收 PHP 传递过来的包含标签的变量,但是此处不能编写标签"}
            <h3>{$vo.name}</h3>
        {/volist}
        <br>
            <h3>5. 循环的 key</h3>
        {volist name="list" id="vo" key="i"}
            <h3>{$i}:{$vo.name}</h3>
        {/volist}
    <h2>二、foreach 循环</h2>
            <h3>1. 普通写法</h3>
        {foreach $list as $value}
            <h3>{$value.email}</h3>
        {/foreach}
            <h3>2. 第二种写法</h3>
        {foreach name="list" as item="value" key="i" }
            <h3>{$value.email}</h3>
        {/foreach}
    <h2>三、for 循环</h2>
        <h3>1. 一般循环</h3>
        {for start='1' end='2'}
            <h3>{$i}</h3>
        {/for}
            <h3>2. 跳跃循环</h3>
        {for start='1' end='4' step='2' name='i'}
            <h3>{$i}</h3>
        {/for}

【叁】 判断语句

<h1>【叁】 判断语句</h1>
    <h2>一、 switch</h2>
        {switch name='Think.get.level'}
        {case value='1'} <h3>level 1</h3> {/case}
        {case value='1|2'} <h3>level 1 or 2</h3> {/case}
        {default /} <h3>default</h3>
        {/switch}
    <h2>二、 range</h2>
        {range name='Think.get.level' value='1,2,3' type='in'}
            <h3>范围类</h3>
        {else /}
            <h3>范围外</h3>
        {/range}
    <h2>三、 range in 简写(同理 notin)</h2>
        {in name='Think.get.level' value='1,2,3'}
            <h3>范围类</h3>
        {else /}
            <h3>范围外</h3>
        {/in}
    <h2>四、 range between 简写(同理 notbetween)</h2>
        {between name='Think.get.level' value='1,10'}
            <h3>范围内</h3>
        {else /}
            <h3>范围外</h3>
        {/between}
    <h2>五、 是否定义</h2>
        {defined name='APP_PATH'}
            <h3>以定义</h3>
        {else/}
            <h3>未定义</h3>
        {/defined}
    <h2>六、 if 判断</h2>
    {if condition="($Think.get.level==1) AND ($Think.get.level==1)"} //OR
    <h3>等于</h3>
    {else/}
    <h3>不等于</h3>
    {/if}

【肆】 其他问题

<h1>【肆】 其他问题</h1>
    <h3>1. 注释之后的’{}‘标签依旧会解析,只不过不会输出。</h3>
    <h3>2. 单引号双引号都没问题</h3>

另有ThinkPHP 模板的布局使用

本文虽拙,却也系作者劳动,转载还请保留本文链接: http://cyleft.com/?p=537



Have any Question or Comment?

发表评论

电子邮件地址不会被公开。 必填项已用*标注