一. 安裝

" /> 女性污污18污污,黄视频在线观看网站,理论片免费欧美片

一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

Smarty模板快速入門

php的世界里已經出現了各式各樣的模板類,但就功能和速度來說Smarty還是一直處于領先地位,因為Smarty的功能相對強大,所以使用起來比其他一些模板類稍顯復雜了一點?,F在就用30分鐘讓您快速入門。

  一. 安裝

    首先打開網頁http://smarty.php.NET/download.php,下載最新版本的Smarty。解壓下載的文件(目錄結構還蠻復雜的)。接下來我演示給大家一個安裝實例,看過應該會舉一反三的。
    (1) 我在根目錄下建立了新的目錄learn/,再在learn/里建立一個目錄smarty/。將剛才解壓縮出來的目錄的libs/拷貝到smarty/里,再在smarty/里新建templates目錄,templates里新建cache/,templates/,templates_c/, config/.

    (2) 新建一個模板文件:index.tpl,將此文件放在learn/smarty/templates/templates目錄下,代碼如下: 
復制代碼 代碼如下:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">  

<title>Smarty</title>  
</head>  
<body>  
{$hello}  
</body>  
</html> 



新建index.php,將此文件放在learn/下: 
復制代碼 代碼如下:<?php  
//引用類文件  
require 'smarty/libs/Smarty.class.php';  

$smarty = new Smarty;  

//設置各個目錄的路徑,這里是安裝的重點  
$smarty->template_dir = "smarty/templates/templates";  

$smarty->compile_dir = "smarty/templates/templates_c";  

$smarty->config_dir = "smarty/templates/config";  
$smarty->cache_dir = "smarty/templates/cache";   

   
//smarty模板有高速緩存的功能,如果這里是true的話即打開caching,但是會造成網頁不立即更新的問題,當然也可以通過其他的辦法解決  
$smarty->caching = false;  

$hello = "Hello World!";  
//賦值  
$smarty->assign("hello",$hello);  

//引用模板文件  
$smarty->display('index.tpl');  

?> 



(3) 執行index.php就能看到Hello World!了。

  二. 賦值

       在模板文件中需要替換的值用大括號{}括起來,值的前面還要加$號。例如{$hello}。這里可以是數組,比如{$hello.item1},{$hello.item2}…
       而php源文件中只需要一個簡單的函數assign(var , value)。
       簡單的例子:
       *.tpl:
       Hello,{$exp.name}! Good {$exp.time}

       *.php:
       $hello[name] = “Mr. Green”;

       $hello[time]=”morning”;
       $smarty->assign(“exp”,$hello);

       output:
       Hello,Mr.Green! Good morning

三. 引用
       網站中的網頁一般header和footer是可以共用的,所以只要在每個tpl中引用它們就可以了。
       示例:*.tpl:
    {include file="header.tpl"}

       {* body of template goes here *}

       {include file="footer.tpl"}

  四. 判斷
       模板文件中可以使用if else等判斷語句,即可以將一些邏輯程序放在模板里。"eq", "ne", "neq", "gt", "lt", "lte", "le",  "gte"  "ge", "is even", "is odd", "is not even", "is not odd", "not", "mod", "div by", "even by", "odd by","==","!=",">", "<","<=",">="這些是if中可以用到的比較??纯淳湍苤朗裁匆馑及伞?

      示例:
      {if $name eq "Fred"}

                     Welcome Sir.

    {elseif $name eq "Wilma"}

                     Welcome Ma'am.   


    {else}
                     Welcome, whatever you are.

    {/if}


  五. 循環

       在Smarty里使用循環遍歷數組的方法是section,如何賦值遍歷都是在模板中解決,php源文件中只要一個assign就能解決問題。
       示例:
{* this example will print out all the values of the $custid array *}

{section name=customer loop=$custid}

              id: {$custid[customer]}<br>
{/section}

OUTPUT:

id: 1000<br>
id: 1001<br>
id: 1002<br>

  六. 常見問題

       Smarty將所有大括號{}里的東西都視為自己的邏輯程序,于是我們在網頁中想插入Javascript函數就需要literal的幫忙了,literal的功能就是忽略大括號{}。
       示例:
{literal} 
       <script language=Javascript> 

             function isblank(field) { 

                       if (field.value == '')  
                               { return false; } 

                       else 
                               { 

                               document.loginform.submit(); 
                               return true; 

                               } 

             } 

       </script> 
{/literal} 

php技術Smarty模板快速入門,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 色播亚洲精品网站 亚洲第一 | 天天天做天天天天爱天天想 | 亚洲无吗视频 | 午夜免费视频网站 | 草色网 | 青青伊人91久久福利精品 | 91精品福利在线观看 | 天天射天天干天天色 | 免费视频一区二区 | 6080yy午夜不卡一二三区久久 | 狠狠久久亚洲欧美专区 | 天天色一色 | 免费看的黄网站 | 亚洲欧洲视频在线观看 | 天天色成人网 | 亚洲xx站| 日本乱人伦片中文三区 | 国产在视频线精品视频 | 亚洲不卡一区二区三区在线 | 免费福利视频网址 | 色网在线 | 91草莓视频在线观看 | 国产一区二区在线播放 | 成人狠狠色综合 | 日本精品久久久久久久 | 国产高清一区二区 | 日韩免费视频播放 | 亚洲激情五月 | 麻豆久久久久久久 | 日韩亚洲国产欧美精品 | 在线欧美a | 国产老鸭窝毛片一区二区 | 水中色av综合 | 国产日韩精品欧美一区喷 | 91av国产精品 | 久久艹综合 | 亚洲第一综合网 | 国产在线观看麻豆91精品免费 | 激情影院成人区免费观看视频 | 天天做天天添天天谢 | 久久99久久99 |