亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊(cè) PHP MVC framework for web development (Smarty usage instructions)

PHP MVC framework for web development (Smarty usage instructions)

Jul 09, 2016 am 09:10 AM
mvc php smarty web development code Open source frame programming programming language software development

<span style="font-size: 15px;"><span style="font-size: 18pt;">      PHP做Web開(kāi)發(fā)的MVC框架(Smarty使用說(shuō)明 )</span><br>一、Smarty簡(jiǎn)明教程 </span>
<span style="font-size: 15px;">1.安裝演示 </span>
<span style="font-size: 15px;"> 下載最新版本的Smarty-3.1.12,然后解壓下載的文件。接下來(lái)演示Smarty自帶的demo例子。 </span>
<span style="font-size: 15px;">(1)下載地址:http://www.smarty.net/download </span>
<span style="font-size: 15px;">(2)在你的WEB服務(wù)器根目錄下建立新目錄,這里我在/var/www下創(chuàng)建yqting/目錄,然后將解壓之后的目錄中的demo/和libs/目錄復(fù)制到/var/www/yqting/目錄下。 <br>(3)這里要特別注意demo/目錄下cache/和template_c/兩個(gè)目錄,<strong><span style="color: #ff0000;">一定要設(shè)置它們 為可讀寫(xiě)權(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)啟動(dòng)apache。在瀏覽器中輸入http://localhost/yqting/demo/index.php ,這樣一個(gè)簡(jiǎn)單的Smarty demo就實(shí)現(xiàn)了。 </span>
<span style="font-size: 15px;">2.Smarty目錄結(jié)構(gòu) </span>
<span style="font-size: 15px;">(1)以/var/www/yqting目錄開(kāi)始分析: </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 ?  自定義的一些實(shí)用插件 </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 ?  自定義的一些實(shí)用插件</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核心功能插件,不需要進(jìn)行修改 </span>
<span style="font-size: 15px;">(2)添加自己定義的插件 </span>
<span style="font-size: 15px;">  上述目錄結(jié)構(gòu)中,其實(shí)最核心的部分是libs/目錄,同時(shí)這部分也是不允許修改的。</span>
<span style="font-size: 15px;">  而要添加自己的插件,一種方法是將自己定義的插件放在libs/plugins/目錄下,另一種方式是 單獨(dú)創(chuàng)建一個(gè)自己plugins/目錄,同時(shí)還要?jiǎng)?chuàng)建cache/、configs/、templates/和templates _c/目錄,而且要保證cache/和templates_c/目錄的可讀寫(xiě)權(quán)限。 </span>
<span style="font-size: 15px;">  不難發(fā)現(xiàn),其實(shí)上述例子中,demo/目錄就是一個(gè)包含了自己定義的插件的完整目錄。 我們可以參照demo/目錄來(lái)實(shí)現(xiàn)自己的程序。 </span>
<span style="font-size: 15px;">3.實(shí)現(xiàn)一個(gè)簡(jiǎ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)限為可讀寫(xiě)。   </span>
<span style="font-size: 15px;">(2)新建一個(gè)模板文件: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>  這段代碼很簡(jiǎn)單,有什么不明白的繼續(xù)往下看,不要著急!每一個(gè)做顯示的.tpl文件都會(huì)對(duì)應(yīng)一個(gè)處理業(yè)務(wù)邏輯的.php文件,下面介紹這個(gè).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的程序部分實(shí)際就是符合php語(yǔ)言規(guī)范的一組代碼,我們依次來(lái)解釋一下:   </span>
<span style="font-size: 15px;">(1)/**/語(yǔ)句:</span>
<span style="font-size: 15px;">  包含的部分為程序篇頭注釋。主要的內(nèi)容應(yīng)該為對(duì)程序的作用,版權(quán)與作者及編寫(xiě)時(shí)間 做一個(gè)簡(jiǎn)單的介紹,這在smarty中不是必需的,但從程序的風(fēng)格來(lái)講,這是一個(gè)好的風(fēng)格。   </span>
<span style="font-size: 15px;">(2)include_once語(yǔ)句:</span>
<span style="font-size: 15px;">  它將安裝到網(wǎng)站的smarty文件包含到當(dāng)前文件中,注意包含的路徑一定要寫(xiě)正確。   </span>
<span style="font-size: 15px;">(3)$smarty = new Smarty(): </span>
<span style="font-size: 15px;">  這一句新建一個(gè)Smarty對(duì)象$smarty,簡(jiǎn)單的一個(gè)對(duì)象的實(shí)例化。   </span>
<span style="font-size: 15px;">(4)$smarty->templates="": </span>
<span style="font-size: 15px;">  這一句指明$smarty對(duì)象使用tpl模板時(shí)的路徑,它是一個(gè)目錄,在沒(méi)有這一句時(shí), Smarty默認(rèn)的模板路徑為當(dāng)前目錄的templates目錄,實(shí)際在寫(xiě)程序時(shí),我們要將這一 句寫(xiě)明,這也是一種好的程序風(fēng)格。   </span>
<span style="font-size: 15px;">(5)$smarty->templates_c="": </span>
<span style="font-size: 15px;">  這一句指明$smarty對(duì)象進(jìn)行編譯時(shí)的目錄。在模板設(shè)計(jì)篇我們已經(jīng)知道Smarty是 一種編譯型模板語(yǔ)言,而這個(gè)目錄,就是它編譯模板的目錄,要注意,如果站點(diǎn)位于linux 服務(wù)器上,請(qǐng)確保 teamplates_c里定義的這個(gè)目錄具有可寫(xiě)可讀權(quán)限,默認(rèn)情況下它的 編譯目錄是當(dāng)前目錄下的templates_c,出于同樣的理由我們將其明確的寫(xiě)出來(lái)。   </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;">  指明在查找模板變量時(shí)的左右分割符。默認(rèn)情況下為"{"與"}",但在實(shí)際中因?yàn)槲覀円?在模板中使用<script>,Script中的函數(shù)定義難免會(huì)使用{},雖然它有自己的解決辦法,但習(xí)慣上我們將它重新定義為"{#"與"#}"或是"<!--{"與"}-->"或其它標(biāo)志符,注意,如果在這里定義了左右分割符后,在模板文件中相應(yīng)的要使每一個(gè)變量使用與定義相同的符號(hào), 例如在這里指定為"<{"與"}>",html模板中也要相應(yīng)的將{$name}變成<{$name}>, 這樣程序才可以正確的找到模板變量。   </script></span>
<span style="font-size: 15px;">(7)$smarty->cache="./cache": </span>
<span style="font-size: 15px;">  告訴Smarty輸出的模板文件緩存的位置。上一篇我們知道Smarty最大的優(yōu)點(diǎn)在于它 可以緩存,這里就是設(shè)置緩存的目錄。默認(rèn)情況下為當(dāng)前目錄下的cache目錄,與 templates_c目錄相當(dāng),在linux系統(tǒng)中,我們要確保它的可讀可寫(xiě)性。   </span>
<span style="font-size: 15px;">(8)$smarty->cache_lifetime = 60 * 60 * 24:   </span>
<span style="font-size: 15px;">  這里將以秒為單位進(jìn)行計(jì)算緩存有效的時(shí)間。第一次緩存時(shí)間到期時(shí)當(dāng)Smarty的 caching變量設(shè)置為true時(shí)緩存將被重建。當(dāng)它的取值為-1時(shí)表示建立起的緩存從不過(guò)期, 為0時(shí)表示在程序每次執(zhí)行時(shí)緩 存總是被重新建立。上面的設(shè)置表示將cache_lifetime設(shè)置為一天。   </span>
<span style="font-size: 15px;">(9)$smarty->caching = 1:   </span>
<span style="font-size: 15px;">  這個(gè)屬性告訴Smarty是否要進(jìn)行緩存以及緩存的方式。</span>
<span style="font-size: 15px;">  它可以取3個(gè)值,0: Smarty默認(rèn)值,表示不對(duì)模板進(jìn)行緩存;1:表示Smarty將使用當(dāng)前定義的 cache_lifetime來(lái)決定是否結(jié)束cache;2:表示 Smarty將使用在cache被建立時(shí)使用 cache_lifetime這個(gè)值。習(xí)慣上使用true與false來(lái)表示是否進(jìn)行緩存。   </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),我們要在后面的例子詳細(xì)的講解這個(gè)成員函數(shù)的使用方法,assign是Smarty的核心函數(shù)之一, 所有對(duì)模板變量的替換都要使用它。   </span>
<span style="font-size: 15px;">(11)$smarty->display("index.tpl"):   </span>
<span style="font-size: 15px;">  該函數(shù)原形為display(string varname),作用為顯示一個(gè)模板。簡(jiǎn)單的講,它將分析 處理過(guò)的模板顯示出來(lái),這里的模板文件不用加路徑,只要使用一個(gè)文件名就可以了,它路 徑我們已 經(jīng)在$smarty->templates(string path)中定義過(guò)了。   </span>
<span style="font-size: 15px;"><br>程序執(zhí)行完后我們可以打開(kāi)當(dāng)前目錄下的templates_c與cache目錄,就會(huì)發(fā)現(xiàn)在下 邊多出一些%%的目錄,這些目錄就是Smarty的編譯與緩存目錄,它由程序自動(dòng)生成,不 要直接對(duì)這些生成的文件進(jìn)行修改。   </span>
<span style="font-size: 15px;">以上我簡(jiǎn)單的把Smarty程序中的一些常用的基本元素介紹了一下,在后邊的例子中你可以看到將它們將被多次的使用。</span>
<span style="font-size: 15px;">?</span>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Object-Relational Mapping (ORM) Performance Tuning in PHP Object-Relational Mapping (ORM) Performance Tuning in PHP Jul 29, 2025 am 05:00 AM

Avoid N 1 query problems, reduce the number of database queries by loading associated data in advance; 2. Select only the required fields to avoid loading complete entities to save memory and bandwidth; 3. Use cache strategies reasonably, such as Doctrine's secondary cache or Redis cache high-frequency query results; 4. Optimize the entity life cycle and call clear() regularly to free up memory to prevent memory overflow; 5. Ensure that the database index exists and analyze the generated SQL statements to avoid inefficient queries; 6. Disable automatic change tracking in scenarios where changes are not required, and use arrays or lightweight modes to improve performance. Correct use of ORM requires combining SQL monitoring, caching, batch processing and appropriate optimization to ensure application performance while maintaining development efficiency.

Building Immutable Objects in PHP with Readonly Properties Building Immutable Objects in PHP with Readonly Properties Jul 30, 2025 am 05:40 AM

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

The Serverless Revolution: Deploying Scalable PHP Applications with Bref The Serverless Revolution: Deploying Scalable PHP Applications with Bref Jul 28, 2025 am 04:39 AM

Bref enables PHP developers to build scalable, cost-effective applications without managing servers. 1.Bref brings PHP to AWSLambda by providing an optimized PHP runtime layer, supports PHP8.3 and other versions, and seamlessly integrates with frameworks such as Laravel and Symfony; 2. The deployment steps include: installing Bref using Composer, configuring serverless.yml to define functions and events, such as HTTP endpoints and Artisan commands; 3. Execute serverlessdeploy command to complete the deployment, automatically configure APIGateway and generate access URLs; 4. For Lambda restrictions, Bref provides solutions.

A Deep Dive into PHP's Internal Garbage Collection Mechanism A Deep Dive into PHP's Internal Garbage Collection Mechanism Jul 28, 2025 am 04:44 AM

PHP's garbage collection mechanism is based on reference counting, but circular references need to be processed by a periodic circular garbage collector; 1. Reference count releases memory immediately when there is no reference to the variable; 2. Reference reference causes memory to be unable to be automatically released, and it depends on GC to detect and clean it; 3. GC is triggered when the "possible root" zval reaches the threshold or manually calls gc_collect_cycles(); 4. Long-term running PHP applications should monitor gc_status() and call gc_collect_cycles() in time to avoid memory leakage; 5. Best practices include avoiding circular references, using gc_disable() to optimize performance key areas, and dereference objects through the ORM's clear() method.

Laravel raw SQL query example Laravel raw SQL query example Jul 29, 2025 am 02:59 AM

Laravel supports the use of native SQL queries, but parameter binding should be preferred to ensure safety; 1. Use DB::select() to execute SELECT queries with parameter binding to prevent SQL injection; 2. Use DB::update() to perform UPDATE operations and return the number of rows affected; 3. Use DB::insert() to insert data; 4. Use DB::delete() to delete data; 5. Use DB::statement() to execute SQL statements without result sets such as CREATE, ALTER, etc.; 6. It is recommended to use whereRaw, selectRaw and other methods in QueryBuilder to combine native expressions to improve security

Java Authentication and Authorization with JWT Java Authentication and Authorization with JWT Jul 29, 2025 am 12:07 AM

JWT is an open standard for safe transmission of information. In Java, authentication and authorization can be achieved through the JJWT library. 1. Add JJWT API, Impl and Jackson dependencies; 2. Create JwtUtil tool class to generate, parse and verify tokens; 3. Write JwtFilter intercepts requests and verify BearerTokens in Authorization header; 4. Register Filter in SpringBoot to protect the specified path; 5. Provide a login interface to return JWT after verifying the user; 6. The protected interface obtains user identity and roles through parsing the token for access control, and ultimately realizes a stateless and extensible security mechanism, suitable for distributed systems.

go by example generics go by example generics Jul 29, 2025 am 04:10 AM

Go generics are supported since 1.18 and are used to write generic code for type-safe. 1. The generic function PrintSlice[Tany](s[]T) can print slices of any type, such as []int or []string. 2. Through type constraint Number limits T to numeric types such as int and float, Sum[TNumber](slice[]T)T safe summation is realized. 3. The generic structure typeBox[Tany]struct{ValueT} can encapsulate any type value and be used with the NewBox[Tany](vT)*Box[T] constructor. 4. Add Set(vT) and Get()T methods to Box[T] without

python json loads example python json loads example Jul 29, 2025 am 03:23 AM

json.loads() is used to parse JSON strings into Python data structures. 1. The input must be a string wrapped in double quotes and the boolean value is true/false; 2. Supports automatic conversion of null→None, object→dict, array→list, etc.; 3. It is often used to process JSON strings returned by API. For example, response_string can be directly accessed after parsing by json.loads(). When using it, you must ensure that the JSON format is correct, otherwise an exception will be thrown.

See all articles