search
HomeBackend DevelopmentPHP TutorialAn article detailing the principles of the PHP template engine (with code examples)

Developing a web project is usually divided into two parts. One part is GUI, that is, interface and art, written using HTML, CSS, JS, and the other part is business logic, that is, programs and functions, written using PHP. The template engine is the "bridge" that connects the two parts. It can be understood as a PHP class, which defines many methods to load the original output of PHP into the interface style and then output it.

The code written before using the template is like this:

<?php $title = “标题”;

$content = “内容”;

?>



<title><?php  echo $title; ?></title>

<?php  echo $content; ?>

In this way, there is no separation between art and logic. Simply put, PHP programmers must not only write programs but also be proficient in art. This It is often very difficult, so the two parts of the work need to be separated.

Let’s create a simple template engine as an example to better understand the principles of the template engine.

As mentioned before, developing a web project is divided into two parts: GUI and business logic, so we create two folders, one named "templates", which stores templates, create a new tpl.html file, and name the other For "PHP", which stores business logic, create a new index.php file.

tpl.html file code usually looks like this:


<title></title>



It can be seen that compared with the previous code, tpl.html only has html code and no php code, but new ones have been added Tags, this type of tag is defined by yourself, and the content inside can only be recognized after being compiled by the template engine. The compiled files are stored in the "templates_c" folder. This file is mainly used for interface design, and will use a large number of CSS, JS and other technologies.

At this time, tpl.html and index.php are still separated and do not interfere with each other, so they need a "bridge" connection, that is, the template engine, which is actually a PHP class, so a mytpl can be created here. PHP file class.php.

The mytpl.class.php file usually defines a class MyTpl. An array tpl_var[] needs to be defined in the class to store the parameters of the custom content tag in tpl.html. In addition, some methods need to be defined. These methods The main purpose is to convert unrecognized content tags in tpl.hml into PHP statements, and then write them to tpl_c.html in a "templates_c" folder. This process is called compilation.

Usually the code of the tpl_c.html file is like this:


<title>
<?php  echo $this->tpl_var[“title”]; ?>

</title><?php  echo $this->tpl_var[“content”]; ?}>

It can be seen that the above code can be recognized because the custom content tags have been replaced with PHP statements. But we still don’t know what the values ​​of title and content are. At this time, the index.php created before will come into play.

index.php Usually its code is like this:

include  “tpl.class.php文件”;//加载模板引擎

$tpl = new MyTpl();//实例化一个模板类

$title = “标题”;

$content = “内容”;

$tpl->assign(“title”,$title);//调用模板类中的方法,分配变量

$tpl->assign(“content”,$content);

$tpl->display(“tpl.html”);//调用模板类中的方法,用于显示编译后的内容

The above code is just some simple description code. The values ​​of the variables can be directly defined or obtained from the database. In addition, the file can also write some complex PHP programs, which is the business mentioned before. logic.

In this way, the work of GUI and business logic is separated. Art designers only need to write template files to change the web interface, while PHP programmers can concentrate on writing their own programs.

Companies generally have their own template engines, and there is usually no need to write the template engine yourself, because there are already some very mature template engines on the market, such as Smarty. We only need to know how to use it. Enough.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of An article detailing the principles of the PHP template engine (with code examples). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:cnblogs. If there is any infringement, please contact admin@php.cn delete
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php怎么去除首位数字php怎么去除首位数字Apr 20, 2022 pm 03:23 PM

去除方法:1、使用substr_replace()函数将首位数字替换为空字符串即可,语法“substr_replace($num,"",0,1)”;2、用substr截取从第二位数字开始的全部字符即可,语法“substr($num,1)”。

PHP编程中有哪些常见的模板引擎?PHP编程中有哪些常见的模板引擎?Jun 12, 2023 am 09:50 AM

最近几年,PHP编程中的模板引擎已经成为了PHP开发的重要组成部分,方便了程序员进行页面开发和管理。本文将介绍PHP编程中常见的模板引擎。SmartySmarty是一个比较常用的PHP模板引擎,它支持缓存模板、插件模块和自定义函数等一系列功能。Smarty的语法十分灵活,能够解决PHP变量与HTML标记的结合难题,使得PHP语言更适用于模板化的设计。而且,S

学习使用Golang模板引擎:在Golang中使用模板的基础指南学习使用Golang模板引擎:在Golang中使用模板的基础指南Jan 20, 2024 am 10:13 AM

Golang模板引擎入门指南:如何在Golang中使用模板,需要具体代码示例简介:模板引擎是一种能将数据和模板进行组合并生成HTML、文本或其他格式文档的工具。在Golang中,我们可以使用内置的模板包(html/template)来实现模板引擎的功能。本文将详细介绍如何在Golang中使用模板引擎,并提供具体的代码示例。一、模板引擎的基本概念在了解如何使用

php怎么去掉右边几个字符php怎么去掉右边几个字符Apr 21, 2022 pm 07:45 PM

去掉方法:1、用substr_replace()删除右边n位置开始的全部字符,语法“substr_replace($str,"",-n)”,参数“n”为需要去除的字符个数;2、用substr(),语法“substr($str,0,-n)”。

php有操作时间的方法吗php有操作时间的方法吗Apr 20, 2022 pm 04:24 PM

php有操作时间的方法。php中提供了丰富的日期时间处理方法:1、date(),格式化本地日期和时间;2、mktime(),返回日期的时间戳;3、idate(),格式化本地时间为整数;4、strtotime(),将时间字符串转为时间戳等等。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft