应用PHP标签模板实现什么任务
我们在对
1、模板的由来
在没有模板技术之前,使用PHP开发程序,通常都是php代码和html混编在一起。比如说新闻列表,很可能就是一个newslist.php页面,结构如下:
- ?
- //从数据库中读取出要显示的新闻记录
- ?>
- html>
- head>……..
- /head>
- body>
- ?
-
While ($news = mysql
_fetch_array($result)) { - ?>
- !--输出新闻标题 -- >
- ?
- }
- ?>
- /body>
- /html>
那么这样有什么问题呢?首先,不利于分工合作。一般来说,都会由程序员来写代码,美工人员设计页面。那么在这种方式中,程序员必须等待美工人员把界面设计 好才能开始工作。也就是说程序员和美工的工作是不能同步的。其次、不利于维护,可维护性差。比如说程序固定后,要修改界面,那么必须由美工修改后,程序员 再重新添加。最后,程序结构混乱,可读性差。HTML和PHP混编在一起,一旦程序一多,就会变的非常的凌乱。
理解模板原理——使用PHP标签模板
模板技术正是为了解决这些问题而出现的,为了解决这些问题,最早出现的是使用PHP标签模板。
首先,我们要明白模板的目的是什么。模板主要要实现几个任务呢?
第一、美工和程序的分离。更确切的说法是获取数据和显示数据的分离。
第二、分工。良好的分工合作。
比如说新闻列表,如果使用PHP标签模板,我们就可以把新闻列表的操作分成两部分:
1、 getnews.php 负责从数据库中读取数据到数组$news中,不关心$news是如何显示的。
2、 Shownews.php 负责把$news数组输出成HTML页面。而它不需要理会$news是从哪里来的。
好,这样,我们就实现了美工和程序的分离,达到了我们的初步目的,但是如何把这两个页面联合起来,并实现listnews.php的功能呢?
这就需要另外一个页面listnews.php,来负责把“美工(显示数据)”和“程序员(获取数据)”连接起来。应该说这个页面就是很简单了。
假设getnews.php的代码如下:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ? </span></span></span></li> <li> <span>$</span><span class="attribute">news</span><span> =“新闻列表”;</span> </li> <li><span>//实际中应该是从数据库中读取出来的。 </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> <li><span>Shownesw.php的代码如下: </span></li> <li class="alt"><span class="tag"><span> </span><span class="tag-name">html</span><span class="tag">></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">title</span><span class="tag">></span><span>显示新闻</span><span class="tag"><span> /title</span><span class="tag">></span><span> </span></span></span></li> <li><span class="tag"><span> /head</span><span class="tag">></span><span> </span></span></li> <li class="alt"><span class="tag"><span> </span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li> <li><span class="tag"><span> ?=$news</span><span class="tag">?></span><span> </span></span></li> <li class="alt"><span class="tag"><span> /body</span><span class="tag">></span><span> </span></span></li> <li><span class="tag"><span> /html</span><span class="tag">></span><span> </span></span></li> </ol>
那么,这个联合页面listnews.php的代码就很简单了
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ? </span></span></span></li> <li><span>Include(‘getnews.php');</span></li> <li><span>//获取数据 </span></li> <li class="alt"><span>Include(‘shownesw.php');</span></li> <li class="alt"><span>//显示数据 </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
总结
使用PHP标签模板系统,能够很好的实现美工和程序的分离,同时方便程序员和美工人员的分工合作,比如在上面的例子中shownews.php由美工人 员来维护,getnews.php由程序人员来维护。而listnews.php就可以由系统设计人员来维护了。当然这中间需要增加一些约定的文档。
事实上,这个简单的例子也说明了最基本的MVC模型。其中M,模型,也就是负责读取数据,相当于我们的getnews.php。V,就是试图,用来显示数据,也就对应了shownews.php。最后是控制器C,对应我们的listnews.php

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

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)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
