zoukankan      html  css  js  c++  java
  • 夺命雷公狗---Smarty NO:23 常用方法

    • assign :分配变量到模板文件(值传递)
    • assignByRef:分配变量到模板文件(引用传递)

    assignByRef代码示例:

    $smarty = new Smarty();
    $name = ‘lisi';
    //$smarty -> assign(‘name’,$name);  值传递相当于把lisi复制一份发送到模板页
    $smarty -> assignByRef(‘name’,$name);//引用传递,把$name变量的首地址赋值给模版页
    $smarty -> display(‘demo7.html’);

    append :分配变量到模板数组变量中

    示例:

    <?php
    require “smarty/Smarty.class.php”;
    $smarty = new Smarty();
    $city = ‘北京';
    //通过append方法分配变量到模版文件
    $smarty -> append(‘city’,$city);
    $smarty -> display(‘demo7.html’);

    改写分页程序,如下所示:

    $sql = “select id,name,age from stu order by id desc limit $offset,$pagesize”;
    $res = mysql_query($sql);
    $data = array();
    while($arr = mysql_fetch_assoc($res)){
    //$data[] = $arr;
    $smarty -> append(‘data’,$arr);
    }
    //通过assign方法分配变量到模版文件
    //$smarty -> assign(‘data’,$data);//遍历出来的结果
    $smarty -> assign(‘pagecount’,$pagecount);//总页数
    • appendByRef:分配变量到模板数组变量中(引用传递)
    • clearAllAssign:清除所有分配到模板中的变量
    • clearAssign:清除指定的模板变量
    • clearCache:清除缓存
    • configLoad:载入配置文件

    引入配置文件如下所示

    <?php
    require “smarty/Smarty.class.php”;
    $smarty = new Smarty();
    $smarty -> configLoad(“config.conf”);
    $smarty -> display(‘demo7.html’);
    • clearConfig:清除配置变量信息
    • display:显示输出模板信息,如下所示

    $smarty -> display(‘demo7.html’);

    • fetch:载入文件到字符串

    demo7.php代码示例:

    <?php
    require “smarty/Smarty.class.php”;
    $smarty = new Smarty();
    $title = ‘test';
    $smarty -> assign(‘title’,$title);
    $str = $smarty->fetch(‘demo7.html’);
    echo $str;

    demo7.html代码示例:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=’utf-8′>
    <title></title>
    <script src=”jquery.js”></script>
    </head>
    <body>
    {$title}
    </body>
    </html>

    在实际项目开发中,fetch拥有非常广泛的用途,例如静态化技术

    //display有两个功能,1.载入到字符串   2.输出字符串echo
    $str = $smarty->fetch(‘demo7.html’);

    file_put_contents(“html/”.time().’.shtml’,$str);

    • templateExists :判断当前模板文件是否存在

    示例代码如下:

    <?php
    header(“Content-Type:text/html;charset=utf-8″);
    require “smarty/Smarty.class.php”;
    $smarty = new Smarty();
    $title = ‘test';
    $smarty -> assign(‘title’,$title);
    if($smarty->templateExists(“demo79.html”)){
        $smarty -> display(“demo7.html”);
    }else{
        echo “系统正在维护中,稍后会为您提供更好的服务…”;
    }
  • 相关阅读:
    Windows 7/8/10 系统下Laravel框架的开发环境安装及部署详解(Vagrant + Homestead)
    简单易懂的laravel事件,这个功能非常的有用(监听事件,订阅者模式)
    搭建docker私有仓库
    CreatarGlobe实现多机立体显示方案(初稿)
    基于3D Vision眼镜的OSG立体显示 【转】
    一天干掉一只Monkey计划(一)——基本光照模型及RT后处理 【转】
    一天干掉一只Monkey计划(序)【转】
    RenderMonkey基本使用方法【转】
    怎样使用libcurl获取隐藏了文件后缀的url网络文件类型
    VR开发者必看:4大最为值得关注的内容平台【转】
  • 原文地址:https://www.cnblogs.com/leigood/p/5033481.html
Copyright © 2011-2022 走看看