Table of Contents
{PAGETITLE}
{PAGE_TITLE}
Home php教程 php手册 在PHP中使用模板的方法

在PHP中使用模板的方法

Jun 13, 2016 pm 12:28 PM
php Why by use Can exist method template of

好了,你可能想知道为什么你要使用FastTemplates。 

·可以在几秒钟改变你的整个站点的外观 
·抽象程序设计,没有垃圾HTML代码 
·设计人员不需要关心全部的"模糊"代码 
·令人惊讶地快 
·更容易重用旧的模版(对普通的表单而说)  

  FastTemplate源于一个有同样名称的Perl软件包(可以在CPAN上找到)。你可以下载PHP 的版本从它的主页(本站下载地址为:http://www.phpe.net/downloads/1.shtml)。你只需要其中的一个类的文件(class.FastTemplate.php)。 



  让我首先解释一下在使用模板生成一个页面与简单地通过echo或print 将页面输出之间有什么不同吧。 
简单地使用echo/print的方法很适合编写短的脚本,但是不能帮助你更好的组织和定制。模板在另一方面给 
了你创建多国语言站点的能力,只是通过改动一个参数。他们可以促使你更关心你要做的。 



  在开始编码之前不要害怕思考。它可能会花费一些时间,但是这些花费会随着项目的发展对你有所回报。 



  那么,如何应用FastTemplate呢?首先你需要先进行一个简单地调用: 

 

传递给它一个路径,是所有你的模板文件存放的目录。它返回一个对象,你可以用它进行参 
数赋值,生成页面等等。  

  FastTemplate是基于这样一种假设:一个很大的页面是由很多小的部分组成的。每一个部分有一个唯一 
的名字。最小的部分是赋值给这样有着唯一名字的一段正常的文本字符串。这个可以通过 
$tpl->assign(NAME, "text"); 
?> 
来完成。现在,如果你的一个模板包含{NAME},FastTemplate 就知道你 
的意图了。 


  另外,FastTemplate需要知道你想如何调用你的模板。你需要通过传递一个相关数组(associative 
array)给 define(); ?> 
来给它一个提示。 
以下为引用的内容:
$tpl->define(array(foo => "foo.tpl", 
bar => "bar.tpl")); 
?>  

  这些赋值将分别给foo和bar以不同的文件(名为foo.tpl和bar.tpl)。  

  现在你想让FastTemplate替换在模板foo中的所有{MACROS}为相应的值。通过发出命令  

以下为引用的内容:
$tpl->parse(PAGECONTENT, "foo"); 
?>  

来实现。 这个命令将把模板"foo"的内容赋给PAGECONTENT。   当然,我们还没有做完,因为模板bar中为主要的页面定义,FastTemplate需要替换其中的 
{PAGECONTENT}宏。我们也需要给PAGETITLE赋值,如下所做: 
以下为引用的内容:  
$tpl->assign(PAGETITLE, "FooBar test"); 
$tpl->parse(MAIN, "bar"); 
?> 

  容易吧,不是吗?我们现在只需要把它输出: $tpl->FastPrint(MAIN); 
?> 
  下面三个文件显示了实际练习中的更多的细节描述。我不知道在现实生活了离了这个技术应如何生活 -- 
你的设计者会高兴,你的老板会微笑,因为你可以在更短的时间内做更多的事情。 



以下为引用的内容:
bar.tpl 
 
 

Feature world - {PAGETITLE} 
 

{PAGETITLE}

 
{PAGECONTENT} 
 
 
foo.tpl  

很明显示什么都没做。请看{NAME}. 
以下为引用的内容: 



demo.php3 
include "class.FastTemplate.php3"; 
$tpl = new FastTemplate( "."); 
$tpl->define(array(foo => "foo.tpl", bar => "bar.tpl"));  

$tpl->assign(NAME, "me"); 
$tpl->assign(PAGETITLE, "Welcome!"); 




$tpl->parse(PAGECONTENT, "foo"); 
$tpl->parse(MAIN, "bar"); 



$tpl->FastPrint(MAIN); 
?>  

创建整个表格 
  我也写了一个短的例子,用来演示如何通过单行模板来生成整个表格。它很有效,因为你仍然不需要直 
接修改HTML文档。 



  我们增加一个模板的内容到一个已经定义过的唯一命名的后面来创建HTML表格。这个可以通过在调用 
$tpl->parse()时,在模板名前加上一个"."来实现。 // 将模板foo的内容赋给TPL1 
$tpl->parse(TPL1, "foo");  

// 在TPL1后附上模板bar的内容 
$tpl->parse(TPL1, ".bar"); 
?>  

page.tpl 




以下为引用的内容:
 
Feature world - {PAGE_TITLE} 
 

{PAGE_TITLE}

 
{PAGE_CONTENT} 
 
  
table.tpl 



以下为引用的内容:
 
    
{TABLE_ROWS} 
namesize
  





table_row.tpl  

以下为引用的内容:
 
{FILENAME} 
{FILESIZE} 
  




yad.php3 



以下为引用的内容:
include "class.FastTemplate.php3"; 
function InitializeTemplates() { 
global $tpl;  

$tpl = new FastTemplate( "."); 
$tpl->define( array( page => "page.tpl", 
table => "table.tpl", 
table_row => "table_row.tpl" ) ); 
}  

function ReadCurrentDirectory() { 
global $tpl;  

$handle = opendir( "."); 
while($filename = readdir($handle)) { 
$tpl->assign(FILENAME, $filename); 
$tpl->assign(FILESIZE, filesize($filename)); 
$tpl->parse(TABLE_ROWS, ".table_row"); 

closedir($handle); 
$tpl->parse(PAGE_CONTENT, "table"); 




function PrintPage($title) { 
global $tpl;  

$tpl->assign(PAGE_TITLE, $title); 
$tpl->parse(FINAL, "page"); 
$tpl->FastPrint(FINAL); 
}  

InitializeTemplates(); 
ReadCurrentDirectory(); 
Printpage( "Yet Another Demo"); 
?>  

速度讨论 




  "Ok," 你可能会说,"一切都太好了。但是它不会影响我的网站的速度吗?" www~ 

  不,你的网站可能变得更快。一个简单的原因就是:因为你作为一个编程人员关心的是设计你的应用和编写代码,你的代码将会更有效率,处理相同的任务更容易和更快速。所以,你可能会在上面列出的为什么考虑使用FastTemplate在你的项目中的原因列表中增加另一条理由。  

  如果你只是想转换一个已经存在的web站点,性能上的成功可能不会被注意到。我建议在PHP中使用正则表达式缓冲,它将对这种情况有所帮助。因为FastTemplate对每一个宏都使用正则表达式,每一个正则表达式将被只编译一次并且速度上的影响可以忽略不计。
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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

What is the difference between php framework laravel and yii What is the difference between php framework laravel and yii Apr 30, 2025 pm 02:24 PM

The main differences between Laravel and Yii are design concepts, functional characteristics and usage scenarios. 1.Laravel focuses on the simplicity and pleasure of development, and provides rich functions such as EloquentORM and Artisan tools, suitable for rapid development and beginners. 2.Yii emphasizes performance and efficiency, is suitable for high-load applications, and provides efficient ActiveRecord and cache systems, but has a steep learning curve.

Steps to add and delete fields to MySQL tables Steps to add and delete fields to MySQL tables Apr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

How to process sensor data in C? How to process sensor data in C? Apr 28, 2025 pm 10:00 PM

C is suitable for processing sensor data due to its high performance and low-level control capabilities. Specific steps include: 1. Data collection: Obtain data through the hardware interface. 2. Data analysis: convert the original data into available information. 3. Data processing: filtering and smoothing processing. 4. Data storage: Save data to a file or database. 5. Real-time processing: Ensure the efficient and low latency of the code.

uniswap on-chain withdrawal uniswap on-chain withdrawal Apr 30, 2025 pm 07:03 PM

Uniswap users can withdraw tokens from liquidity pools to their wallets to ensure asset security and liquidity. The process requires gas fees and is affected by network congestion.

How to configure the character set and collation rules of MySQL How to configure the character set and collation rules of MySQL Apr 29, 2025 pm 04:06 PM

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

See all articles