PHP做Web開發(fā)的MVC框架(Smarty使用說明 )
Jul 09, 2016 am 09:10 AM<span style="font-size: 15px;"><span style="font-size: 18pt;"> PHP做Web開發(fā)的MVC框架(Smarty使用說明 )</span><br>一、Smarty簡明教程 </span>
<span style="font-size: 15px;">1.安裝演示 </span>
<span style="font-size: 15px;"> 下載最新版本的Smarty-3.1.12,然后解壓下載的文件。接下來演示Smarty自帶的demo例子。 </span>
<span style="font-size: 15px;">(1)下載地址:http://www.smarty.net/download </span>
<span style="font-size: 15px;">(2)在你的WEB服務器根目錄下建立新目錄,這里我在/var/www下創(chuàng)建yqting/目錄,然后將解壓之后的目錄中的demo/和libs/目錄復制到/var/www/yqting/目錄下。 <br>(3)這里要特別注意demo/目錄下cache/和template_c/兩個目錄,<strong><span style="color: #ff0000;">一定要設置它們 為可讀寫權(quán)限</span></strong>。 </span>
<span style="font-size: 15px;"> chmod 777 cache/ </span>
<span style="font-size: 15px;"> chmod 777 template_c/ </span>
<span style="font-size: 15px;">(4)啟動apache。在瀏覽器中輸入http://localhost/yqting/demo/index.php ,這樣一個簡單的Smarty demo就實現(xiàn)了。 </span>
<span style="font-size: 15px;">2.Smarty目錄結(jié)構(gòu) </span>
<span style="font-size: 15px;">(1)以/var/www/yqting目錄開始分析: </span>
<span style="font-size: 15px;"> yqting/ </span>
<span style="font-size: 15px;"> ├── demo </span>
<span style="font-size: 15px;"> │ ├── cache 緩沖文件存放目錄 </span>
<span style="font-size: 15px;"> │ ├── configs ? 配置文件目錄 </span>
<span style="font-size: 15px;"> │ ├── index.php </span>
<span style="font-size: 15px;"> │ ├── plugins ? 自定義的一些實用插件 </span>
<span style="font-size: 15px;"> │ ├── templates ? 模板目錄 </span>
<span style="font-size: 15px;"> │ └── templates_c 編譯后的文件存放目錄 </span>
<span style="font-size: 15px;"> └── libs</span>
<span style="font-size: 15px;">?? ├── debug.tpl debug模板 </span>
<span style="font-size: 15px;">?? ├── plugins ? 自定義的一些實用插件</span>
<span style="font-size: 15px;">? ? ├── SmartyBC.class.php 支持Smarty 2兼容 </span>
<span style="font-size: 15px;"> ?? ├── Smarty.class.php Smarty類定義文件 </span>
<span style="font-size: 15px;">? ? └── sysplugins ? Smarty核心功能插件,不需要進行修改 </span>
<span style="font-size: 15px;">(2)添加自己定義的插件 </span>
<span style="font-size: 15px;"> 上述目錄結(jié)構(gòu)中,其實最核心的部分是libs/目錄,同時這部分也是不允許修改的。</span>
<span style="font-size: 15px;"> 而要添加自己的插件,一種方法是將自己定義的插件放在libs/plugins/目錄下,另一種方式是 單獨創(chuàng)建一個自己plugins/目錄,同時還要創(chuàng)建cache/、configs/、templates/和templates _c/目錄,而且要保證cache/和templates_c/目錄的可讀寫權(quán)限。 </span>
<span style="font-size: 15px;"> 不難發(fā)現(xiàn),其實上述例子中,demo/目錄就是一個包含了自己定義的插件的完整目錄。 我們可以參照demo/目錄來實現(xiàn)自己的程序。 </span>
<span style="font-size: 15px;">3.實現(xiàn)一個簡單的例子 </span>
<span style="font-size: 15px;">(1)在/var/www/yqting/下創(chuàng)建目錄weibo/,然后在weibo/目錄下創(chuàng)建cache/、 configs/、templates/和templates_c/目錄,修改cache/和templates_c/目錄的權(quán)限為可讀寫?! ?</span>
<span style="font-size: 15px;">(2)新建一個模板文件:index.tpl,將此文件放在/var/www/yqting/weibo/templates目錄下,代碼如下: </span>
<span style="font-size: 15px;"> </span>
<span style="font-size: 15px;"> </span>
<span style="font-size: 15px;"> <metahttp-equiv content="text/html;charset=gb2312"></metahttp-equiv></span>
<span style="font-size: 15px;"> <title>Smarty</title> </span>
<span style="font-size: 15px;"> </span>
<span style="font-size: 15px;"> </span>
<span style="font-size: 15px;"> username:{$Name}</span>
<span style="font-size: 15px;"> </span>
<span style="font-size: 15px;"> <br> 這段代碼很簡單,有什么不明白的繼續(xù)往下看,不要著急!每一個做顯示的.tpl文件都會對應一個處理業(yè)務邏輯的.php文件,下面介紹這個.php文件。</span>
<span style="font-size: 15px;">(3)新建index.php,將此文件放在/var/www/yqting/weibo/下,代碼如下: </span>
<span style="font-size: 15px;"> <?php <br> </span><span style="font-size: 15px;">/*sample example */<br> </span><span style="font-size: 15px;">require '../libs/Smarty.class.php'; <br> </span><span style="font-size: 15px;">$smarty = new Smarty(); <br> </span><span style="font-size: 15px;">$username = "Smarty"; <br> $smarty->assign("Name",$username); <br> </span><span style="font-size: 15px;">$smarty->display('index.tpl'); <br> </span><span style="font-size: 15px;">?> <br> 其中require使用的路徑一定要正確,可以參照上面的目錄結(jié)構(gòu)看一下!</span>
<span style="font-size: 15px;">(4)在Smarty3中,Smarty類的構(gòu)造函數(shù)中已經(jīng)指定了template_dir、compile_dir 、config_dir和cache_dir,不需要再次指定。 </span>
<span style="font-size: 15px;">(5) 在瀏覽器中輸入http://localhost/yqting/weibo/index.php,就可以看到輸出的信息username:Smarty 了。 </span>
<span style="font-size: 15px;">二、解釋smarty的程序 </span>
<span style="font-size: 15px;"> 我們可以看到,smarty的程序部分實際就是符合php語言規(guī)范的一組代碼,我們依次來解釋一下: </span>
<span style="font-size: 15px;">(1)/**/語句:</span>
<span style="font-size: 15px;"> 包含的部分為程序篇頭注釋。主要的內(nèi)容應該為對程序的作用,版權(quán)與作者及編寫時間 做一個簡單的介紹,這在smarty中不是必需的,但從程序的風格來講,這是一個好的風格。 </span>
<span style="font-size: 15px;">(2)include_once語句:</span>
<span style="font-size: 15px;"> 它將安裝到網(wǎng)站的smarty文件包含到當前文件中,注意包含的路徑一定要寫正確。 </span>
<span style="font-size: 15px;">(3)$smarty = new Smarty(): </span>
<span style="font-size: 15px;"> 這一句新建一個Smarty對象$smarty,簡單的一個對象的實例化。 </span>
<span style="font-size: 15px;">(4)$smarty->templates="": </span>
<span style="font-size: 15px;"> 這一句指明$smarty對象使用tpl模板時的路徑,它是一個目錄,在沒有這一句時, Smarty默認的模板路徑為當前目錄的templates目錄,實際在寫程序時,我們要將這一 句寫明,這也是一種好的程序風格。 </span>
<span style="font-size: 15px;">(5)$smarty->templates_c="": </span>
<span style="font-size: 15px;"> 這一句指明$smarty對象進行編譯時的目錄。在模板設計篇我們已經(jīng)知道Smarty是 一種編譯型模板語言,而這個目錄,就是它編譯模板的目錄,要注意,如果站點位于linux 服務器上,請確保 teamplates_c里定義的這個目錄具有可寫可讀權(quán)限,默認情況下它的 編譯目錄是當前目錄下的templates_c,出于同樣的理由我們將其明確的寫出來。 </span>
<span style="font-size: 15px;">(6)<strong><span style="color: #ff0000; font-size: 16px;">分隔符 $smarty->left_delimiter與$smarty->right_delimiter: </span></strong></span>
<span style="font-size: 15px;"> 指明在查找模板變量時的左右分割符。默認情況下為"{"與"}",但在實際中因為我們要 在模板中使用<script>,Script中的函數(shù)定義難免會使用{},雖然它有自己的解決辦法,但習慣上我們將它重新定義為"{#"與"#}"或是"<!--{"與"}-->"或其它標志符,注意,如果在這里定義了左右分割符后,在模板文件中相應的要使每一個變量使用與定義相同的符號, 例如在這里指定為"<{"與"}>",html模板中也要相應的將{$name}變成<{$name}>, 這樣程序才可以正確的找到模板變量。 </script></span>
<span style="font-size: 15px;">(7)$smarty->cache="./cache": </span>
<span style="font-size: 15px;"> 告訴Smarty輸出的模板文件緩存的位置。上一篇我們知道Smarty最大的優(yōu)點在于它 可以緩存,這里就是設置緩存的目錄。默認情況下為當前目錄下的cache目錄,與 templates_c目錄相當,在linux系統(tǒng)中,我們要確保它的可讀可寫性。 </span>
<span style="font-size: 15px;">(8)$smarty->cache_lifetime = 60 * 60 * 24: </span>
<span style="font-size: 15px;"> 這里將以秒為單位進行計算緩存有效的時間。第一次緩存時間到期時當Smarty的 caching變量設置為true時緩存將被重建。當它的取值為-1時表示建立起的緩存從不過期, 為0時表示在程序每次執(zhí)行時緩 存總是被重新建立。上面的設置表示將cache_lifetime設置為一天。 </span>
<span style="font-size: 15px;">(9)$smarty->caching = 1: </span>
<span style="font-size: 15px;"> 這個屬性告訴Smarty是否要進行緩存以及緩存的方式。</span>
<span style="font-size: 15px;"> 它可以取3個值,0: Smarty默認值,表示不對模板進行緩存;1:表示Smarty將使用當前定義的 cache_lifetime來決定是否結(jié)束cache;2:表示 Smarty將使用在cache被建立時使用 cache_lifetime這個值。習慣上使用true與false來表示是否進行緩存。 </span>
<span style="font-size: 15px;">(10)$smarty->assign("name", $username): </span>
<span style="font-size: 15px;"> 該數(shù)的原型為assign(string varname, mixed var),varname為模板中使用的模板變量,var指出要將模板變量替換的變量名;其第二種原形為assign(mixed var),我們要在后面的例子詳細的講解這個成員函數(shù)的使用方法,assign是Smarty的核心函數(shù)之一, 所有對模板變量的替換都要使用它。 </span>
<span style="font-size: 15px;">(11)$smarty->display("index.tpl"): </span>
<span style="font-size: 15px;"> 該函數(shù)原形為display(string varname),作用為顯示一個模板。簡單的講,它將分析 處理過的模板顯示出來,這里的模板文件不用加路徑,只要使用一個文件名就可以了,它路 徑我們已 經(jīng)在$smarty->templates(string path)中定義過了。 </span>
<span style="font-size: 15px;"><br>程序執(zhí)行完后我們可以打開當前目錄下的templates_c與cache目錄,就會發(fā)現(xiàn)在下 邊多出一些%%的目錄,這些目錄就是Smarty的編譯與緩存目錄,它由程序自動生成,不 要直接對這些生成的文件進行修改。 </span>
<span style="font-size: 15px;">以上我簡單的把Smarty程序中的一些常用的基本元素介紹了一下,在后邊的例子中你可以看到將它們將被多次的使用。</span>
<span style="font-size: 15px;">?</span>

熱AI工具

Undress AI Tool
免費脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動的應用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機

Video Face Swap
使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級代碼編輯軟件(SublimeText3)

避免N 1查詢問題,通過提前加載關(guān)聯(lián)數(shù)據(jù)來減少數(shù)據(jù)庫查詢次數(shù);2.僅選擇所需字段,避免加載完整實體以節(jié)省內(nèi)存和帶寬;3.合理使用緩存策略,如Doctrine的二級緩存或Redis緩存高頻查詢結(jié)果;4.優(yōu)化實體生命周期,定期調(diào)用clear()釋放內(nèi)存以防止內(nèi)存溢出;5.確保數(shù)據(jù)庫索引存在并分析生成的SQL語句以避免低效查詢;6.在無需跟蹤變更的場景下禁用自動變更跟蹤,改用數(shù)組或輕量模式提升性能。正確使用ORM需結(jié)合SQL監(jiān)控、緩存、批量處理和適當優(yōu)化,在保持開發(fā)效率的同時確保應用性能。

ReadonlypropertiesinPHP8.2canonlybeassignedonceintheconstructororatdeclarationandcannotbemodifiedafterward,enforcingimmutabilityatthelanguagelevel.2.Toachievedeepimmutability,wrapmutabletypeslikearraysinArrayObjectorusecustomimmutablecollectionssucha

Laravel支持使用原生SQL查詢,但應優(yōu)先使用參數(shù)綁定以確保安全;1.使用DB::select()執(zhí)行帶參數(shù)綁定的SELECT查詢,防止SQL注入;2.使用DB::update()執(zhí)行UPDATE操作并返回影響行數(shù);3.使用DB::insert()插入數(shù)據(jù);4.使用DB::delete()刪除數(shù)據(jù);5.使用DB::statement()執(zhí)行如CREATE、ALTER等無結(jié)果集的SQL語句;6.推薦在QueryBuilder中使用whereRaw、selectRaw等方法結(jié)合原生表達式以提升安

首先通過JavaScript獲取用戶系統(tǒng)偏好和本地存儲的主題設置,初始化頁面主題;1.HTML結(jié)構(gòu)包含一個按鈕用于觸發(fā)主題切換;2.CSS使用:root定義亮色主題變量,.dark-mode類定義暗色主題變量,并通過var()應用這些變量;3.JavaScript檢測prefers-color-scheme并讀取localStorage決定初始主題;4.點擊按鈕時切換html元素上的dark-mode類,并將當前狀態(tài)保存至localStorage;5.所有顏色變化均帶有0.3秒過渡動畫,提升用戶

Go泛型從1.18開始支持,用于編寫類型安全的通用代碼。1.泛型函數(shù)PrintSlice[Tany](s[]T)可打印任意類型切片,如[]int或[]string。2.通過類型約束Number限制T為int、float等數(shù)字類型,實現(xiàn)Sum[TNumber](slice[]T)T安全求和。3.泛型結(jié)構(gòu)體typeBox[Tany]struct{ValueT}可封裝任意類型值,配合NewBox[Tany](vT)*Box[T]構(gòu)造函數(shù)使用。4.為Box[T]添加Set(vT)和Get()T方法,無需

json.loads()用于將JSON字符串解析為Python數(shù)據(jù)結(jié)構(gòu),1.輸入必須是雙引號包裹的字符串且布爾值為true/false;2.支持null→None、對象→dict、數(shù)組→list等自動轉(zhuǎn)換;3.常用于處理API返回的JSON字符串,如response_string經(jīng)json.loads()解析后可直接訪問嵌套數(shù)據(jù),使用時需確保JSON格式正確,否則會拋出異常。

@property裝飾器用于將方法轉(zhuǎn)為屬性,實現(xiàn)屬性的讀取、設置和刪除控制。1.基本用法:通過@property定義只讀屬性,如area根據(jù)radius計算并直接訪問;2.進階用法:使用@name.setter和@name.deleter實現(xiàn)屬性的賦值驗證與刪除操作;3.實際應用:在setter中進行數(shù)據(jù)驗證,如BankAccount確保余額非負;4.命名規(guī)范:內(nèi)部變量用_前綴,property方法名與屬性一致,通過property統(tǒng)一訪問控制,提升代碼安全性和可維護性。

使用datetime.strptime()可將日期字符串轉(zhuǎn)換為datetime對象,1.基本用法:通過"%Y-%m-%d"解析"2023-10-05"為datetime對象;2.支持多種格式如"%m/%d/%Y"解析美式日期、"%d/%m/%Y"解析英式日期、"%b%d,%Y%I:%M%p"解析帶AM/PM的時間;3.可用dateutil.parser.parse()自動推斷未知格式;4.使用.d
