木心

毕竟几人真得鹿,不知终日梦为鱼

导航

smarty模板引擎

1、新建一个新项目smartyDemo

  不使用thinkphp。仅仅是作为一个简单演示demo。

  配置虚拟主机:

  

  conf/extra/httpd-vhosts.conf:

#注意:要将下面4行注释掉
#<VirtualHost _default_:80>
#DocumentRoot "${SRVROOT}/htdocs"
#ServerName www.example.com:80
#</VirtualHost>

# Add any other Virtual Hosts below
<VirtualHost *:80>
    ServerName web.abc123.com
    DocumentRoot "D:/workspaces/phpDemo01/"
    <Directory "D:/workspaces/phpDemo01/">
        #Options Indexes
        #AllowOverride All
        #Order Deny,Allow
        #Allow From All
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
#    ServerName dummy-host2.example.com
#    ErrorLog "logs/dummy-host2.example.com-error.log"
#    CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerName web.abc456.com
    DocumentRoot "D:/workspaces/phpDemo01/smartyDemo/"
    <Directory "D:/workspaces/phpDemo01/smartyDemo/">
        #Options Indexes
        #AllowOverride All
        #Order Deny,Allow
        #Allow From All
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
#    ServerName dummy-host2.example.com
#    ErrorLog "logs/dummy-host2.example.com-error.log"
#    CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>

 

2、案例演示

<?php
    include("./libs/Smarty.class.php");
    $smarty = new Smarty();
    $smarty -> template_dir = "./template";
    $smarty -> compile_dir =  "./template_c";
    $smarty -> assign('title', 'smarty标题');
    $smarty -> assign('content', 'smarty内容');

    $stu = array("id" => 1, "name" => "张三丰", "age" => 100);
    $smarty -> assign('stu', $stu);
    $smarty -> display('smarty01.html');

 

  下载smarty:https://www.smarty.net/download,下载后解压,将lib文件夹的内容复制到本项目根目录下,并新建template和template_c两个文件夹。

  

  index.php:

<?php
    include("./libs/Smarty.class.php");
    $smarty = new Smarty();
    $smarty -> template_dir = "./template";
    $smarty -> compile_dir =  "./template_c";
    $smarty -> assign('title', 'smarty标题');
    $smarty -> assign('content', 'smarty内容');

    $stu = array("id" => 1, "name" => "张三丰", "age" => 100);
    $smarty -> assign('stu', $stu);
    $smarty -> display('smarty01.html');

 

  template/smarty01.html:

<!DOCTYPE html>
<html>
<head>
    <title>{$title}</title>
</head>
<body>
    <h1>{$content}</h1>
    {* 这是注释 *}
    <ul>
        <li>{$stu['id']}</li>
        <li>{$stu['name']}</li>
        <li>{$stu['age']}</li>
    </ul>
    <ul>
        <li>{$stu.id}</li>
        <li>{$stu.name}</li>
        <li>{$stu.age}</li>
    </ul>
</body>
</html>

 

  浏览器输入http://web.abc456.com/index.php结果:

 

3、使用资源文件

  新建资源文件configs/resource.conf:

 

  在模板文件index.php中使用资源:

<!DOCTYPE html>
<html>
<head>
    <title>{$title}</title>
</head>
<body>
    <h1>{$content}</h1>
    {* 这是注释 *}
    <ul>
        <li>{$stu['id']}</li>
        <li>{$stu['name']}</li>
        <li>{$stu['age']}</li>
    </ul>
    <ul>
        <li>{$stu.id}</li>
        <li>{$stu.name}</li>
        <li>{$stu.age}</li>
    </ul>

    {* 引入资源文件 *}
    {config_load file = "resource.conf"}
    {#key1#}
    {#key2#}
    <br/>
    
    {config_load file = "resource.conf" section="section1"}
    {#key1#}
    {#key2#}
</body>
</html>

 

4、内置函数if/else、foreach

  index.php:

<?php
    include("./libs/Smarty.class.php");
    $smarty = new Smarty();
    $smarty -> template_dir = "./template";
    $smarty -> compile_dir =  "./template_c";
    $smarty -> assign('title', 'smarty标题');
    $smarty -> assign('content', 'smarty内容');

    $stu = array("id" => 1, "name" => "张三丰", "age" => 100);
    $smarty -> assign('stu', $stu);
    // $smarty -> display('smarty01.html');

    $smarty -> assign('score', "85");

    $smarty -> assign('stuList', array(
        array("id" => 1, "name" => "张无忌", "age" => 30),
        array("id" => 2, "name" => "周芷若", "age" => 20),
        array("id" => 3, "name" => "赵敏", "age" => 20)
    ));

    $smarty -> display('smarty02.html');

 

  模板smarty02.html:

<!DOCTYPE html>
<html>
<head>
    <title>{$title}</title>
</head>
<body>
    <h1>{$content}</h1>
    {* 测试if/else *}
    你的分数是:{$score}
    你的等级:
    {if $score > 100 || $score < 0} 分数错误
    {elseif $score >= 90} A
    {elseif $score >= 80} B
    {elseif $score >= 70} C
    {elseif $score >= 60} D
    {else}  E
    {/if}

    {* 测试foreach 一维数组 *}
    <ul>
        {foreach $stu as $k => $v}
        <li>{$k}--{$v}</li>
        {/foreach}
    </ul>

    {* 测试foreach 二维数组 *}
    <table border = 1 cellspacing = '0' cellpadding = '10'>
        <tr>
            <td>编号</td>
            <td>姓名</td>
            <td>年龄</td>
        </tr>
        {foreach $stuList as $k => $v}
        <tr>
            <td>{$v.id}</td>
            <td>{$v.name}</td>
            <td>{$v.age}</td>
        </tr>
        {/foreach}
    </table>

</body>
</html>

 

  结果显示:

  

 5、变量修饰

<!DOCTYPE html>
<html>
<head>
    <title>{$title}</title>
</head>
<body>
    {* 测试 *}
    {$smarty.now}<br/>
    {$smarty.now|date_format:'%Y-%m-%d %T'}<br/>
    
    {$longStr}<br/>
    {* UTF-8用三个字节表示一个汉字 *}
    {$longStr|truncate:33}<br/>
    {$longStr|truncate:33:'***'}
</body>
</html>

  结果:

  

  truncate的详细用法参考:

  (1)https://blog.csdn.net/forest_fire/article/details/50943411

  (2)https://blog.csdn.net/aidenliu/article/details/5653597

  

posted on 2019-04-23 21:14  wenbin_ouyang  阅读(476)  评论(0编辑  收藏  举报