zoukankan      html  css  js  c++  java
  • smarty函数

    smarty里面包含两种函数:

    一种是普通函数,一种是块函数。

    用smarty模板从数据库读取数据在前台显示数据:

    .php页面:

    <?php
    require "../DBDA.class.php";
    require "../init.inc.php";
    
    
    $db = new DBDA();
    $sql = "select * from info";
    $arr = $db->query($sql);
    
    $smarty->assign("shuju",$arr);
    
    
    
    $smarty->display("test.html"); 

    .html页面:

    <body>
    
    <table width="100%" border="1">
        <tr>
            <td>代号</td>
            <td>姓名</td>
            <td>性别</td>
            <td>民族</td>
            <td>生日</td>
        </tr>
        
        <{foreach $shuju as $v}>
        <tr>
            <td><{$v[0]}></td>
            <td><{$v[1]}></td>
            <td>
                <{if $v[2]}><{else}><{/if}>
            </td>
            <td><{$v[3]}></td>
            <td><{$v[4]}></td>
        </tr>
        <{/foreach}>
        
    </table>
    
    </body>
    </html>

    <{ literal }>标签:

    只要是被<{ literal }>括起来的含有定界符的就不会被解析掉。

    <{literal}>
    <{foreach $shuju as $v}> <tr> <td><{$v[0]}></td> <td><{$v[1]}></td> <td> <{if $v[2]}><{else}><{/if}> </td> <td><{$v[3]}></td> <td><{$v[4]}></td> </tr> <{/foreach}>

    <{/literal}>

    几个插件:

    //在plugins文件夹里:

    我们可以找一些比较好用的工具,来当作这个smarty的插件,拿过来直接用就行。根据函数的调用方式直接调用就可以了

    <body>
    
    
    <{date name="date"}>     //普通函数
    <{color name="color"}>
    
    <{textarea  name="txt" toolbar="full" color="red"}>      //块函数   块函数需要两个,一个开始一个结束。
    <{/textarea}>
    
    
    </body>

    写完函数名之后后面是跟参数。

    自定义函数:

    制作一个可以直接调用一个函数后可以输出一行1的函数

    //首先要想自定义一个函数,就先在project文件夹里面的plugins插件文件夹里面新建一个叫function.shuchu.php的文件。文件里的内容如下:

    <?php
    function smarty_function_shuchu($args,$smarty){
        return "11111111111111111111111111";
    }
    
    
    
    //$args是一个数组,里面包含所有给的参数。
    //括号内的$smarty是smarty对象。
    //如果不传入参数的话,那么$args就是一个空数组。

    //再去.html页面直接调用刚定义好的文件就可以了。

    <body>
    
    
    <{shuchu}>     //直接调用刚自定义好的shuchu函数。
    
    
    </body>

    smarty模板的做法: 

    login.php页面

    <?php
    require "../init.inc.php";
    $smarty->display("login.html");
    
    
    //用smarty模板,路径不再是以后看到的路径了。虽然在.html文件上编辑代码,但路径是以.php文件的路径开始的。

    login.html页面

    <body>
    
    
    <form action="loginchuli.php" method="post"> 
    <div>用户名:<input type="text" name="uid" /></div>
    <div>密码:<input type="text" name="pwd" /></div>
    <input type="submit" value="登录" />
    </form>

    </body> </html>

    loginchuli.php

    <?php
    //这个页面是通过login.html打过来处理的。这个页面是不需要显示的。
    //不需要显示就不需要引入  require "../init.inc.php";  这句话,引入它的目的是
    //为了实现前后端分离,但这个loginchuli.php就没有前端页面。
    
    require "../DBDA.class.php";
    $uid = $_POST["uid"];
    $pwd = $_POST["pwd"];
    
    
    $db = new DBDA();
    $sql = "select pwd from users where uid='{$uid}'";
    $mm = $db->strquery($sql);
    
    if($mm==$pwd && !empty($pwd))
    {
        header("location:test.php");
    }

    通过这三个页面来使用smarty模板。

    总结:

      如果当前这个文件要显示,就会有两个文件,一个前端一个后端页面。这个后端 login.php页面就使用了smarty模板。引入了init.inc.php这个配置文件

    加上display方法来调用login.html页面过来显示。还有loginchuli.php这种页面它因为没有前端页面,所以不牵扯到什么前后端分离。这个loginchuli.php页面

    就不需要引入init.inc.php文件,直接去操作数据库就完了。

    //后台处理页面只有一个文件,不需要用smarty模板。

    //smarty模板和tp框架里面是不允许页面里面嵌入php代码的。因为它把这个网页直接做成了一个静态网页.html。这个.html这种文件是不允许嵌入php代码的

    ,嵌入了也不会解析。所以说smarty和tp的方式是不允许嵌代码的。

     

  • 相关阅读:
    二叉树
    树的存储表示
    Jarvis OJ | WEB
    xctf | pwn进阶
    《C++Primer Plus》 | 处理数据
    xctf---stack2 | gdb&IDA 调试
    IDA | Windows主机与Ubuntu 16.04远程调试
    ROP | 中级
    IDA | 窗口
    epub reading
  • 原文地址:https://www.cnblogs.com/shandayuan/p/7399691.html
Copyright © 2011-2022 走看看