PHP模板Smarty的具體使用指南
Jun 13, 2016 am 11:07 AM
通常我們的解決方法是由美工設(shè)計頁面后交付程序設(shè)計者進(jìn)行開發(fā),再交付美工對頁面進(jìn)行改善,來回重復(fù)好幾回,如果遇到程序設(shè)計者對HTML不熟悉,對雙方來說更是個痛苦的差事,效率也更低下,這時候如果有模板支持就顯得非常重要。
我們知道PHP語言作為開源社區(qū)的一員,提供了各種模板引擎,如FastTemplate,Smarty,SimpleTemplate等,而Smarty是現(xiàn)在使用得比較多的PHP模板引擎,今天和大家分享在PHP開發(fā)中如何安裝與使用PHP模板Smarty,也算是對Smarty的入門學(xué)習(xí)。
準(zhǔn)備工作
1、選擇安裝Smarty的目錄
如果擁有服務(wù)器權(quán)限,考慮到安全性可以選擇將Smarty安裝在WEB程序文檔目錄之外的地方,然后通過將Smarty安裝目錄地址包含在PHP.INI文件中的include_path選項。
如果是虛擬主機(jī)權(quán)限,或者好幾個項目,可以將Smarty安裝在各自的項目目錄中,在require Smarty類文件,也可以使用Smarty模板引擎。當(dāng)然為了安全考慮,你可以通過apache禁止相關(guān)目錄訪問。
另外這兩種PHP模板Smarty安裝方式在移植性方面有所區(qū)別,第一種方式需要保證每臺服務(wù)器有相同的Smarty配置,第二種方式對每臺服務(wù)器配置沒有影響。你可以根據(jù)各自的需要選擇Smarty的安裝方式。
2、下載Smarty,,我選擇的是Smarty-2.6.25
PHP模板Smarty安裝步驟
1、解壓下載的Smarty-2.6.25壓縮包
2、拷貝libs文件夾到WEB程序目錄下,我的目錄為testsmarty
Linux下的安裝方法可以參考這里。
在安裝完Smarty模板后,我們開始簡單使用Smarty。
PHP模板Smarty使用
1、創(chuàng)建相關(guān)目錄
由于在使用Smarty的過程中,Smarty會生成編譯的模板文件以及其他配置文件、緩存文件,我們需要創(chuàng)建相關(guān)的目錄。我在testsmarty目錄下,另外創(chuàng)建了tpls目錄,并在tpls目錄下創(chuàng)建templates、templates_c、configs、cache目錄。為什么需要創(chuàng)建這些目錄呢?打開Smarty.class.php文件,我們可以看到Smarty類定義了部分的成員屬性。
$template_dir:設(shè)定所有模板文件都需要放置的目錄地址。默認(rèn)情況下,目錄是:“./templates”,也就是在PHP執(zhí)行程序同一個目錄下尋找該模板目錄。
$compile_dir:設(shè)定Smarty編譯過的所有模板文件的存放目錄地址。默認(rèn)目錄是:“./templates_c”,也就是在PHP執(zhí)行程序同一個目錄下尋找該編譯目錄。如果你在Linux服務(wù)器上創(chuàng)建這個目錄,你還需要修改此目錄的權(quán)限,使它有寫的權(quán)限。
$config_dir:設(shè)定用于存放模板特殊配置文件的目錄,默認(rèn)目錄是:“./configs”,也就是在PHP執(zhí)行程序同一個目錄下尋找該配置目錄。
$cache_dir:在啟動緩存特性的情況下,這個屬性所指定的目錄中放置PHP模板Smarty緩存的所有模板。默認(rèn)目錄是:”./cache”,也就是在PHP執(zhí)行程序同一個目錄下尋找該緩存目錄。你也可以用你自定義的緩存處理函數(shù)來控制緩存文件,它將會忽略這項設(shè)置。同樣如果你在Linux服務(wù)器上創(chuàng)建這個目錄,你還需要修改此目錄的權(quán)限,使它有寫的權(quán)限。
為了系統(tǒng)安全和移植性考慮,建議不要將這些目錄建立在PHP執(zhí)行程序同一目錄下,可以將它建立在PHP執(zhí)行程序目錄之外,如果已建立在PHP執(zhí)行程序同一目錄下,可以通過Apache做好目錄限制訪問工作。
2、建立相關(guān)配置文件
我們需要創(chuàng)建一個配置文件,來覆蓋PHP模板Smarty類的默認(rèn)成員屬性,并命名為main.php,保存在smarty目錄下,以后哪個腳本需要使用Smarty,我們只要把main.php包含進(jìn)來即可。
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span>?? ?</span></span></span></li> <li><span>include("./smarty/libs/Smarty.class.php"); ?</span></li> <li class="alt"><span>define('SMARTY_ROOT',?'./smarty/tpls'); ?</span></li> <li> <span>$</span><span class="attribute">tpl</span><span>?=?</span><span class="attribute-value">new</span><span>?Smarty(); ?</span> </li> <li class="alt"> <span>$tpl-</span><span class="tag">></span><span class="attribute">template_dir</span><span>?=?</span><span class="attribute-value">SMARTY_ROOT</span><span>."/templates/"; ?</span> </li> <li> <span>$tpl-</span><span class="tag">></span><span class="attribute">compile_dir</span><span>?=?</span><span class="attribute-value">SMARTY_ROOT</span><span>."/templates_c/"; ?</span> </li> <li class="alt"> <span>$tpl-</span><span class="tag">></span><span class="attribute">config_dir</span><span>?=?</span><span class="attribute-value">SMARTY_ROOT</span><span>."/configs/"; ?</span> </li> <li> <span>$tpl-</span><span class="tag">></span><span class="attribute">cache_dir</span><span>?=?</span><span class="attribute-value">SMARTY_ROOT</span><span>."/cache/"; ?</span> </li> <li class="alt"> <span>$tpl-</span><span class="tag">></span><span class="attribute">caching</span><span>=</span><span class="attribute-value">1</span><span>; ?</span> </li> <li> <span>$tpl-</span><span class="tag">></span><span class="attribute">cache_lifetime</span><span>=</span><span class="attribute-value">60</span><span>*60*24; ?</span> </li> <li class="alt"> <span>$tpl-</span><span class="tag">></span><span class="attribute">left_delimiter</span><span>?=?</span><span class="attribute-value">'<span>; ?</span></span> </li> <li> <span>$tpl-</span><span class="tag">></span><span class="attribute">right_delimiter</span><span>?=?</span><span class="attribute-value">'}>'</span><span>; ?</span> </li> <li class="alt"> <span class="tag">?></span><span>?</span> </li> </ol>
注釋:
第1-8行:主要定義一個smarty對象,同時設(shè)定模板文件、編譯文件、緩存文件、配置文件的存放目錄,覆蓋Smarty.class.php中的默認(rèn)值。
第9-10行:設(shè)定開啟緩存,同時設(shè)定緩存的有效時間為1天。
知識點(diǎn):$caching用來設(shè)置是否開啟緩存功能。默認(rèn)值設(shè)為0或無效。你也可以為同一個模板設(shè)有多個緩存,當(dāng)值為1或2時啟動緩存。1告訴Smarty使用當(dāng)前的$cache_lifetime變量判斷緩存是否過期。2告訴PHP模板Smarty使用生成緩存時的cache_lifetime值。建議在項目開發(fā)過程中關(guān)閉緩存,將值設(shè)置為0
第11-12行:設(shè)置smarty語言的左右結(jié)束符,我們知道大括號是smarty的默認(rèn)定界符,但在和javascript、css等結(jié)合時可能會產(chǎn)生沖突,所以這里我們設(shè)定為。
3、建立一個模板文件
一般情況下在美工頁面設(shè)計完畢后,雙方的交集點(diǎn)是模版文件,雙方約定后,程序員不需要花太大的精力在前臺,這就是使用Smarty模板引擎進(jìn)行開發(fā)的好處。
我們首先建立一個簡單的模版文件,名為leapsoul.tpl,你可在html文件中加入smarty變量后將文件另存為tpl類型的文件。
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span>?</span><span class="tag-name">html</span><span class="tag">></span><span>?</span></span></span></li> <li><span class="tag"><span>?</span><span class="tag-name">head</span><span class="tag">></span><span>?</span></span></li> <li class="alt"><span class="tag"><span>?</span><span class="tag-name">meta</span><span>?</span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"Content-type"</span><span>?<br></span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html;?charset=gb2312"</span><span class="tag">></span><span>?</span></span></li> <li><span class="tag"><span>?</span><span class="tag-name">title</span><span class="tag">></span><span>? ?</span></span></li> <li class="alt"><span class="tag"><span>?{?$title?}</span><span class="tag">></span><span>?</span></span></li> <li><span class="tag"><span>?/title</span><span class="tag">></span><span>? ?</span></span></li> <li class="alt"><span class="tag"><span>?/head</span><span class="tag">></span><span>? ?</span></span></li> <li> <span>?</span><span class="tag"><span class="tag-name">body</span><span class="tag">></span><span>? ?</span></span> </li> <li class="alt"><span class="tag"><span>?{?$content?}</span><span class="tag">></span><span>? ?</span></span></li> <li><span class="tag"><span>?/body</span><span class="tag">></span><span>? ?</span></span></li> <li class="alt"><span class="tag"><span>?/html</span><span class="tag">></span><span>?</span></span></li> </ol>
注釋:在這個tpl文件中設(shè)定了title和content兩個PHP模板Smarty變量,文件保存為leapsoul.tpl,同時將其保存在testsmartytplstemplates模板文件目錄下。
4、建立應(yīng)用程序文件
模版文件類似于一個表現(xiàn)層,在建立完模板文件后,我們需要一個應(yīng)用程序去驅(qū)動表現(xiàn)層,應(yīng)用程序文件定義為smarty.php。
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span>?? ?</span></span></span></li> <li><span>include("smarty/main.php"); ?</span></li> <li class="alt"> <span>$tpl-</span><span class="tag">></span><span>assign("title",?"leapsoul.cn為你<br>展示smarty模板技術(shù)"); ?</span> </li> <li> <span>$tpl-</span><span class="tag">></span><span>assign("content",?"leapsoul.cn<br>通過詳細(xì)的安裝使用步驟為你展示smarty模板技術(shù)"); ?</span> </li> <li class="alt"> <span>$tpl-</span><span class="tag">></span><span>display("leapsoul.tpl"); ?</span> </li> <li> <span class="tag">?></span><span>?</span> </li> </ol>
注釋:
在這段代碼中我們主要用到smarty中的兩個函數(shù)assign和display,assign你可以理解為為變量賦值,display主要是用來將網(wǎng)頁輸出。更多smarty函數(shù)今后會詳細(xì)介紹。
其他說明
由于我們開啟了緩存功能,有興趣的朋友可以打開cache和templates_c,cache目錄存放了這個模板的緩存文件,文件開頭部分有緩存信息,如文件的生成時間和過期時間等,其他的和一般的HTML文件沒有多大的區(qū)別,而templates_c存放了模板經(jīng)過編譯后的PHP執(zhí)行文件。
至此一個簡單入門的PHP模板Smarty應(yīng)用實(shí)例就算介紹完成了。?

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

The core role of Homebrew in the construction of Mac environment is to simplify software installation and management. 1. Homebrew automatically handles dependencies and encapsulates complex compilation and installation processes into simple commands; 2. Provides a unified software package ecosystem to ensure the standardization of software installation location and configuration; 3. Integrates service management functions, and can easily start and stop services through brewservices; 4. Convenient software upgrade and maintenance, and improves system security and functionality.
