zoukankan      html  css  js  c++  java
  • 夺命雷公狗---Smarty NO:03 设计篇1

    1、模板注释

    基本语法:

    {* Smarty注释 *}

    demo.php

    示例代码:

    <?php
    //1.加载smarty项目入口文件
    require “smarty/Smarty.class.php”;
    //2.创建smarty对象
    $smarty = new Smarty();
    //3.初始化信息
    //4.通过assign方法去分配到模版文件
    //5.通过display方法显示输出信息
    $smarty -> display(‘demo.html’);

    demo.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=’utf-8′>
    <title></title>
    </head>
    <body>
    {*这是在smarty里面的注释方法噢,嘻嘻*}
    </body>
    </html>

    说明:Smarty中模板注释属于服务端注释,所以其被不会输出显示在客户端浏览器的源码中。

    2、模板中的变量

    1)从PHP中分配的变量

    2)从配置文件中读取变量

    3)在模板文件中创建

    1)从PHP中分配的变量

    普通变量、数组、对象

    示例代码:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=’utf-8′>
    <title></title>
    </head>
    <body>
    {*这是在smarty里面的注释方法噢,嘻嘻*}
    {*普通变量*}
    {$str}
    <hr/>
    {*一维数组*}
    <ul>
    <li>{$lamp[0]}</li>
    <li>{$lamp[1]}</li>
    <li>{$lamp[2]}</li>
    <li>{$lamp[3]}</li>
    </ul>
    <hr/>
    {*多维数组*}
    <table border=’1′>
    <tr>
    <td>姓名</td>
    <td>年龄</td>
    <td>性别</td>
    </tr>
    <tr>
    <td>{$persons[0][0]}</td>
    <td>{$persons[0][1]}</td>
    <td>{$persons[0][2]}</td>
    </tr>
    </table>
    <hr/>
    {*对象类型*}
    {$stu->speak()}
    </body>
    </html>

    demo.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=’utf-8′>
    <title></title>
    </head>
    <body>
    {*This is a Smarty Template*}
    {*普通变量*}
    {$str}
    <hr/>
    {*一维数组*}
    <ul>
    <li>{$lamp[0]}</li>
    <li>{$lamp[1]}</li>
    <li>{$lamp[2]}</li>
    <li>{$lamp[3]}</li>
    </ul>
    <hr/>
    {*多维数组*}
    <table border=’1′>
    <tr>
    <td>姓名</td>
    <td>年龄</td>
    <td>性别</td>
    </tr>
    <tr>
    <td>{$persons[0][0]}</td>
    <td>{$persons[0][1]}</td>
    <td>{$persons[0][2]}</td>
    </tr>
    </table>
    <hr/>
    {*对象类型*}
    {$stu->speak()}
    </body>
    </html>

    2)从配置文件中读取变量

    在Smarty中,配置主要用于页面信息的输出显示

    demo02.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=’utf-8′>
    <title></title>
    </head>
    <body>
    {config_load file=’config.conf’}
    {#title#} <!–引入配置文件中的变量–>
    </body>
    </html>

    configs/config.conf

    title=Hello Smarty

    3)在模板中直接定义变量

    基本语法:

    {assign var=’变量名称’ value=’变量的值’}

    {$变量名称=’变量的值’}

    调用形式如下:

    {$变量名称}

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=’utf-8′>
    <title></title>
    </head>
    <body>
    {*自定义变量定义的两种方式*}
    {assign var = ‘name’ value = ‘lisi’}
    {$age = ’55’}
    {*自定义变量的调用*}
    {$name}
    {$age}
    </body>
    </html>

    3、Smarty中的常用保留变量

    在Smarty系统,通过$smarty开头的变量都属于系统保留变量

    $smarty.get.page :相当于PHP中的$_GET[‘page’];

    $smarty.post.page :相当于$_POST[‘page’];

    $smarty.cookies.username :$_COOKIE[‘username’];

    $smarty.server.SERVER_NAME :$_SERVER[‘SERVER_NAME’];

    $smarty.env.Path :$_ENV[‘Path’];

    $smarty.session.id :$_SESSION[‘id’];

    $smarty.request.username :$_REQUEST[‘username’];

    {$smarty.now} :返回系统时间的时间戳

    {$smarty.const} :返回系统中的常量信息

    {$smarty.capture} :输出capture标签所捕获的内容

    {$smarty.config} :获取配置文件中的信息

    {$smarty.section} :读取section循环信息

    {$smarty.template} :读取当前正在操作的模板信息

    {$smarty.current_dir} :读取当前目录

    {$smarty.version} :读取当前Smarty版本号

    {$smarty.ldelim} :读取左分界符

    {$smarty.rdelim} :读取右分界符

    注:如需要使用ENV环境变量,请在php.ini文件中,设置一下变量:

    示例代码:

    demo02.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=’utf-8′>
    <title></title>
    </head>
    <body>
    读取页面中的GET信息:{$smarty.get.page}
    <hr/>
    读取页面中的session信息:{$smarty.session.username}
    <hr/>
    读取smarty版本:{$smarty.version}
    <hr/>
    读取系统时间的时间戳:{$smarty.now}
    <hr/>
    读取系统中的环境变量Path:{$smarty.env.Path}
    </body>
    </html>

    demo02.php

    <?php
    require “smarty/Smarty.class.php”;
    $smarty = new Smarty();
    $_SESSION[‘username’] = ‘admin';
    $smarty -> display(“demo2.html”);
  • 相关阅读:
    使用 libevent 和 libev 提高网络应用性能
    An existing connection was forcibly closed by the remote host
    各种浏览器的兼容css
    vs输出窗口,显示build的时间
    sass
    网站设置404错误页
    List of content management systems
    css footer not displaying at the bottom of the page
    强制刷新css
    sp_executesql invalid object name
  • 原文地址:https://www.cnblogs.com/leigood/p/5032969.html
Copyright © 2011-2022 走看看