PHP Smarty Template Engine working mechanism (i)

Source: Internet
Author: User
Tags auth php code smarty template

Without using the Smarty template engine, we all edit the PHP program and the page template together, like the following source code:

The code is as follows Copy Code
<?php
$title = "Deep smarty template engine working mechanism";
$content = "Smarty Template engine principle flowchart";
$auth = "Marcofly";
$website = "Www.111cn.net";
?>
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title><?php Echo $title?></title>
<body>
<p> content: <?php echo $content?></p>
<p> Author: <?php Echo $auth?></p>
<p> URL: <?php echo $website?></p>
</body>

HTML code

The code is as follows Copy Code

<! DOCTYPE html>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">

<title> Deep Smarty Template engine working mechanism </title>

<body>

<p> content: Smarty Template engine Principle flow chart </p>

<p> author:marcofly</p>

</body>

This development is inconvenient when the program is small, and once you have to develop a large Web project, you have to use the template engine.

Using the template engine: our way of development will change, the artists just do the template, the background developers focus on their own programs. A Web project can be divided into template files and PHP programs such as: The artist can edit the page template file like this:

The code is as follows Copy Code
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title><{$title}></title>
<body>
<p> content: <{$content}></p>
<p> Author: <{$auth}></p>
<p> website: <{$website}></p>
</body>
From the above two simple demo code can be seen, the foreground template file does not involve any code about PHP, only a few seemingly unfamiliar tags <{$title}> and <{$content}>, And the background of the PHP program code is not related to the front of the HMTL code
Refer to the following figure to compare the differences between the two development methods

By contrast, we conclude that the place where PHP was originally written using the template engine is now replaced with the form of a template engine that provides a label .
Note: The default label form for the Smarty template engine is {$xxx}, for example, {$title},{$content}
Of course we can initialize the label form we want, as I initialize it to: <{$xxx}>, such as <{$title}>, <{$content}>
I do not know you reader have wood feel strange, <{$title}>, <{$content}> is not the syntax of PHP at all, that is how the final output to the customer's browser, is there another mystery? With this question, we continue to delve into ...
In fact, all of this is by Smarty template engine this pair of mysterious hands in the "covert operation", after the Smarty template engine "covert operation", the initial template file (index.dwt) after Smarty "successful surgery", is transformed into a PHP code file that can be executed on the server side.
Want to see the template file (INDEX.DWT) and the background of the PHP program (index.php) after the "surgery" (that is, compiled) after the truth?
Here is the source code for the compiled file after the template engine has been compiled:
 
  
  
  code is as follows copy code
 <! DOCTYPE html> 
<meta http-equiv= "Content-type" content= text/html; charset =utf-8 ""
<title><?php Echo $this->arr["title"]? ></title>
<body
<p> content: <?php echo $this->arr["content"]? ></p>
<p> Author: <?php Echo $this-> arr["Auth"]? ></p>
<p> URL: <?php echo $this->arr["website"]? ></p>
</body>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.