Home > Article > Backend Development > An 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.
<?php $title = “标题”; $content = “内容”; ?> <html> <head><title><?php echo $title; ?></title></head> <body><?php echo $content; ?></body> </html>
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:
<html> <head><title><{$title}></title></head> <body><{ $content;}></body> </html>
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.
<html> <head><title><?php echo $this->tpl_var[“title”]; ?></head> <body><?php echo $this->tpl_var[“content”]; ?}></body> </html>
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.
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!