Home>Article>Backend Development> An article detailing the principles of the PHP template engine (with code examples)

An article detailing the principles of the PHP template engine (with code examples)

藏色散人
藏色散人 forward
2022-08-07 14:59:47 3525browse

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 echo $title; ?>

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}>
<{ $content;}>

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:

 <?php echo $this->tpl_var[“title”]; ?><script type="text/javascript" src="/js/jquery.3.5.2.min.m.js"></script>
</head><div style="position: fixed;right: 0;top:100px;width: 125px; z-index:2000;"><div ><a target="_blank" rel="nofollow" href="https://www.520xingyun.com/from/188bet.php" ><img width="120px" height="550px" src="https://www.520xingyun.com/images/188_120.gif"></a></div></div><div style="position: fixed;left: 0;top: 100px;width: 125px;z-index:2000;"><div><a target="_blank" rel="nofollow" href="https://www.520xingyun.com/from/188bet.php"><img width="120px" height="550px" src="https://www.520xingyun.com/images/188_120.gif"></a></div></div> <body><?php echo $this->tpl_var[“content”]; ?}></body> </html></pre>
      <p>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.<br></p>
      <h3 id="indexphp通常它代码是这样的">index.php Usually its code is like this:</h3>
      <pre class="brush:php;toolbar:false">include “tpl.class.php文件”;//加载模板引擎 $tpl = new MyTpl();//实例化一个模板类 $title = “标题”; $content = “内容”; $tpl->assign(“title”,$title);//调用模板类中的方法,分配变量 $tpl->assign(“content”,$content); $tpl->display(“tpl.html”);//调用模板类中的方法,用于显示编译后的内容</pre>
      <p>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.</p>
      <p>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.</p>
      <p>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.</p>
      <p>Recommended learning: "<a href="//m.sbmmt.com/m/course/list/29.html" target="_blank">PHP Video Tutorial</a>"<br></p>
      <p>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!</p>
     </div>
     <div class="nphpQianMsg">
      <div class="clear"></div>
     </div>
     <div class="nphpQianSheng">
      <span>Statement:</span>
      <div>
       This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete
      </div>
     </div>
    </div>
    <div class="nphpSytBox">
     <span>Previous article:<a class="dBlack" title="The latest brief analysis of PHP features, kernel and architecture in 2022" href="//m.sbmmt.com/m/faq/494709.html">The latest brief analysis of PHP features, kernel and architecture in 2022</a></span>
     <span>Next article:<a class="dBlack" title="The latest brief analysis of PHP features, kernel and architecture in 2022" href="//m.sbmmt.com/m/faq/494711.html">The latest brief analysis of PHP features, kernel and architecture in 2022</a></span>
    </div>
    <div class="nphpSytBox2">
     <div class="nphpZbktTitle">
      <h2>Related articles</h2>
      <em><a href="//m.sbmmt.com/m/article.html" class="bBlack"><i>See more</i><b></b></a></em>
      <div class="clear"></div>
     </div>
     <ul class="nphpXgwzList">
      <li><b></b><a href="//m.sbmmt.com/m/faq/453789.html" title="What is the principle of php template engine?" class="aBlack">What is the principle of php template engine?</a>
       <div class="clear"></div></li>
      <li><b></b><a href="//m.sbmmt.com/m/faq/456528.html" title="Detailed explanation of the method and math function for generating random numbers in PHP template engine smarty" class="aBlack">Detailed explanation of the method and math function for generating random numbers in PHP template engine smarty</a>
       <div class="clear"></div></li>
      <li><b></b><a href="//m.sbmmt.com/m/faq/485057.html" title="Let’s talk about what PHP template engines have" class="aBlack">Let’s talk about what PHP template engines have</a>
       <div class="clear"></div></li>
      <li><b></b><a href="//m.sbmmt.com/m/faq/493105.html" title="Which template engine does thinkphp use by default?" class="aBlack">Which template engine does thinkphp use by default?</a>
       <div class="clear"></div></li>
     </ul>
    </div>
   </div>
   <div class="nphpFoot">
    <div class="nphpFootBg">
     <ul class="nphpFootMenu">
      <li><a href="//m.sbmmt.com/m/"><b class="icon1"></b><p>Home</p></a></li>
      <li><a href="//m.sbmmt.com/m/course.html"><b class="icon2"></b><p>Course</p></a></li>
      <li><a href="//m.sbmmt.com/m/wenda.html"><b class="icon4"></b><p>Q&A</p></a></li>
      <li><a href="//m.sbmmt.com/m/login"><b class="icon5"></b><p>My</p></a></li>
      <div class="clear"></div>
     </ul>
    </div>
   </div>
   <div class="nphpYouBox" style="display: none;">
    <div class="nphpYouBg">
     <div class="nphpYouTitle">
      <span onclick="$('.nphpYouBox').hide()"></span>
      <a href="//m.sbmmt.com/m/"></a>
      <div class="clear"></div>
     </div>
     <ul class="nphpYouList">
      <li><a href="//m.sbmmt.com/m/"><b class="icon1"></b><span>Home</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/course.html"><b class="icon2"></b><span>Course</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/article.html"><b class="icon3"></b><span>Article</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/wenda.html"><b class="icon4"></b><span>Q&A</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/dic.html"><b class="icon6"></b><span>Dictionary</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/course/type/99.html"><b class="icon7"></b><span>Manual</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/xiazai/"><b class="icon8"></b><span>Download</span>
        <div class="clear"></div></a></li>
      <li><a href="//m.sbmmt.com/m/faq/zt" title=""><b class="icon12"></b><span>Topic</span>
        <div class="clear"></div></a></li>
      <div class="clear"></div>
     </ul>
    </div>
   </div>
   <div class="nphpDing" style="display: none;">
    <div class="nphpDinglogo">
     <a href="//m.sbmmt.com/m/"></a>
    </div>
    <div class="nphpNavIn1">
     <div class="swiper-container nphpNavSwiper1">
      <div class="swiper-wrapper">
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/">Home</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/article.html" class="hover">Article</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/wenda.html">Q&A</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/course.html">Course</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/faq/zt">Topic</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/xiazai">Download</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/game">Game</a>
       </div>
       <div class="swiper-slide">
        <a href="//m.sbmmt.com/m/dic.html">Dictionary</a>
       </div>
       <div class="clear"></div>
      </div>
     </div>
     <div class="langadivs">
      <a href="javascript:;" class="bg4 bglanguage"></a>
      <div class="langadiv">
       <a onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan " href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a>
       <a onclick="javascript:;" class="language course-right-orders chooselan chooselanguage" href="javascript:;"><span>English</span><span>(EN)</span></a>
       <a onclick="javascript:setlang('zh-tw');" class="language course-right-orders chooselan " href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a>
       <a onclick="javascript:setlang('ja');" class="language course-right-orders chooselan " href="javascript:;"><span>日本語</span><span>(JA)</span></a>
       <a onclick="javascript:setlang('ko');" class="language course-right-orders chooselan " href="javascript:;"><span>한국어</span><span>(KO)</span></a>
       <a onclick="javascript:setlang('ms');" class="language course-right-orders chooselan " href="javascript:;"><span>Melayu</span><span>(MS)</span></a>
       <a onclick="javascript:setlang('fr');" class="language course-right-orders chooselan " href="javascript:;"><span>Français</span><span>(FR)</span></a>
       <a onclick="javascript:setlang('de');" class="language course-right-orders chooselan " href="javascript:;"><span>Deutsch</span><span>(DE)</span></a>
      </div>
     </div>
    </div>
   </div>
   <!--顶部导航 end-->
  </div>
  <link rel="stylesheet" id="_main-css" href="//m.sbmmt.com/m/static/css/viewer.min.css" type="text/css" media="all">
 </body>
</html>