zoukankan      html  css  js  c++  java
  • Smarty的入门使用

    Smarty的入门使用27课

    Smarty的运用:变量与循环
    一:
    $smarty->assign("模板变量","值或数组变量")
    $smarty->display("模板名称")
    eg:
    $smarty->assign("name","php100中文站");//进行模板变量替换
    $smarty->display("index.htm") //该文件就是模板文件,在templates下
     
    二:
    $smarty->assign("模板变量","数组");
    {section name=s loop=$stu}
    {$stu[s].name}
    {sectionelse}
    无内容
    {/section}
     

    Smarty变量操作符28课

    capitalize [首字母大写]
    count_characters [计算字符数]
    cat [连接字符串]
    count_paragraphs [计算段落数]
    count_sentences [计算句数]
    count_words [计算词数]
    date_format [时间格式]    //strtotime('-0')   {$name|date_foramt:'/%y-%m-%d'}
    default [默认]            //{$name|default:"没有时间"}
    escape [转码]
    indent[缩进]
    lower[小写 ]
    nl2br[换行符替换成<br />]
    regex_replace[正则替换]
    replace[替换]             //{$name|replace:"is":"**"}
    spacify[插空]
    string_format[字符串格式化]   //浮点:{$name|string_format:'%.2f'}    %d 整数
    strip[去除(多余空格)]
    strip_tags[去除html标签]
    truncate[截取]            //{$name|truncate:10:"..."}
    upper[大写]
    wordwrap[行宽约束]

    Smarty内置函数29课

     

    index.php
     
    <?php
    include("smarty_inc.php");
    $value=array('a'=>'PHP','b'=>'JAVA','c'=>'C++');
    $smarty->assign('name',$value);
    $smarty->display("index.htm");
    ?>
     
    index.htm
    循环运用
    {foreach from=$name item=id key=k}
    数组内容:{$k}-{$id}<br>
    {/foreach}

    {include file='head.htm' title='这是个首页'}
    {if $name==''}
    什么都没有
    {else}
    里面有东西
    {/if}
    数据当作文本处理
    {literal}
    <script language=javascript>
    <!--
    function isblank(filed)
    {
    if(field.value=='')
    {return false;}
    else
    {
    document.loginform.submit();
    return true
    }
    }
    alert("成功");
    //-->
    </script>
    {/literal}
    删除空格
    {strip}
    <table>
      <tr>
        <td>
        <a href="">
        <font color="red">This is a test</font>
        </a>
        </td>
      </tr>
    </table>
    {/strip}

     

    Smarty缓存的运用30课

    这节有难度!
    Smarty缓存的运用30课【php100教程】 - luge0126 - 大灰狼的博客
    项目图
    Smarty缓存的运用30课【php100教程】 - luge0126 - 大灰狼的博客
    index.php
    include("smarty_inc.php");
    include("mysql_inc.php");
     if($_GET[id]){  //判断
     $sql="SELECT * FROM `php100` WHERE id=".$_GET[id];
     $query=$db->query($sql);
     $row=$db->fetch_row($query);
     $db->query('update php100 set hit=hit+1 where id='.$_GET[id]);
     }
     function insert_hit(){  //此项不执行缓存
       global $row;
       return $row[2];

     }
     $smarty->assign('row',$row);
     $smarty->display('index.htm');
     
    index.htm
    标题:{$row[1]}<br>
    点击:{insert name="hit"}次
     
    外加缓存清除方法
    $smarty->clear_all_cache();
    $smarty->clear_cache('index.htm',$id);
     
    function insert_shijian(){
      return date("Y-m-d H:m:s");
     }

     

     

     

     

  • 相关阅读:
    HTML
    JavaScript-DOM
    Visual Studio Usage
    Char 05 使用提供程序
    Char 04 运行命令
    Char 03 使用帮助系统
    char 02 初识 Powershell
    Char 01 PowerShell
    Linux Nginx
    Linux SSLH
  • 原文地址:https://www.cnblogs.com/zhouwenwu/p/2437817.html
Copyright © 2011-2022 走看看