"PHP template Engine" Prototype prototype release!

Source: Internet
Author: User
Tags api manual php oop php template php tutorial smarty template
At the beginning of the article, we must first say sorry to the people who have been concerned about me! It was originally intended to complete the first edition of the PHP template engine immediately after the front end Framework 5.0 was released. But I was not able to do, and has been dragged to 15 years of New Year's Day to complete, there is very serious procrastination I am ashamed, again sorry!

I have said that after the publication of the Post will be published in sync with the corresponding API usage instructions, but I think this is not good enough and the blog platform for the processing and display of the table is not very friendly, resulting in the API is not a perfect presentation, so the intention is to provide only API links, you can directly access to my website through the link to the manual That kind of reading experience is the best. The published article later updated some of the API-related use of teaching, to provide some small examples, so that we can read the API, but also through practical examples to more profound understanding of the various uses of code and business logic ideas!

The following is a link to an API manual and an example demo

"Example Demo":http://www.shibuyi.net/demo/php/template_engine/prototype

"Official API User's Manual":http://www.shibuyi.net/api/php/template_engine/prototype

"Author's own official website":http://www.shibuyi.net

Below is the guide for PHP template engine Prototype prototype, the following direct abbreviation: Template engine!

What the? Don't know what the template engine is? There may be a novice PHP will ask such a question, then I will simply answer it. Template engine is actually a PHP middleware technology, so that the traditional Web Dynamic Web site programming has become easier, why? Was it not easy before? That's because before the template engine appears, almost all WEB server programmers work through a mixed code, and the so-called mash-up is a page that contains both: PHP and even other language code, so if the project expands gradually, the cost of development and maintenance is high and error-prone. , the art and program is not good match, if the artist does not understand the program, there is basically no way to see the mixed file. The emergence of template engine is a good solution to this problem. It uses the template engine technology to separate the PHP business Logic layer code in the mixed file with the presentation layer code HTML, and the programmer can design the program with peace of mind, while the design interface is designed with a special symbol of a template tag. It is convenient for the template engine to compile after reading the template file. PHP already has a lot of quite mature template engine, such as: Smarty template engine, and so on, other Web server language also has its own corresponding template engine, or similar to the template engine middleware technology.

About the template engine detailed introduction, recommend everyone visit Baidu Encyclopedia:http://baike.baidu.com/view/4258079.htm

Before learning the template engine, you have to make sure that you already understand the PHP OOP (Object oriented programming) idea, because template technology is described in OOP, if you do not understand OOP then this article is not suitable for you to read, because the threshold is too high!

The first picture that you see is the template engine directory structure diagram (the author uses the Zendstudio 7.2.1 Integrated development Environment):

1. Caches is the template cache directory (if there is no template engine first execution will be automatically generated);

2. Classes is the template engine core class library;

3. Compiles the template to compile the directory (if there is no template engine first execution will be automatically generated);

4. Constants is the template constant directory (if there is no template engine the first execution will be automatically generated);

5. Includes to the template configuration directory;

6. Templates is the template file directory (automatically generated if no template engine is present for the first execution).

After knowing the catalog of the template engine, let's take a look at how to get him up and running. In the prototype version of the template engine related initialization and configuration information is done in the includes/template.inc.php file (you can open the source code for review).

In fact, the configuration of the template engine is called the initialization process, the first step of initialization is to configure the appropriate directory, so that the template engine can correctly read and write data in the directory (configuration steps Everyone can choose freely, not necessarily in my order to configure, but must be done before instantiating the template engine, Otherwise it will fail), and we assume that the template engine is configured in the same directory, and if the configuration of the root directory is not the same directory, it needs to be tuned.

1. The first step is to configure the root directory of the template engine and automatically generate the absolute path to the root directory if not set.

$rootPath dirname (__file__//  relative and absolute paths are available, we use absolute paths here!) such as: "C:/wwwroot/prototype"

2. Configure the template file directory, this directory is used to store the template file, if not set the default is: Templates directory.

// here is the default directory, everyone according to their own needs to set up, it is recommended not to write Chinese error, the name of the directory before and after the addition of the pros and cons are irrelevant, the final template engine inside will automatically correct!  $templateName = '/templates/';

3. Configure the compiled file directory, which is used to present the template file is parsed after the generated compilation file, if not set the default is: Compiles directory.

$compileName // The default directory is also used in the same way as the template directory. 

4. Configure the cache file directory, which is the cache file that is used to generate the compiled file after the template engine turns on the cache feature, if the default is not set to: Caches directory.

$cacheName // The same as using the default

5. Configure the template constant directory, you may not understand the template constants are used for what, and ordinary PHP constants what is the difference? The explanation of the template constants, in the following application in our detailed discussion, here is the first configuration with me, if not set the default is: Constants directory.

$constantName // Use the default

6. Until the sixth step, the configuration of the directory is complete, you do not have to worry about the directory does not exist, and do not have to create manually, the template engine will automatically help us to complete. Then the next step is to set the file name of the template constant, if not set, the default is: Default.xml file.

// We also use the default, but it is important to note that there must be. xml as an extension, because the constant file is described in XML tags, and if it is not the end of the. XML, it can cause the template engine to handle constants when the exception occurs!  $constantFile = ' default.xml ';

7. Set the cache switch, the cache is closed by default, only if we go to set it, it will be turned on.

// Everybody notice, here I write is a Boolean value, actually here can fill any value, the end will be implicitly converted to Boolean value, write 0 or 1 can, I directly write Boolean value is to facilitate everyone's understanding!  $cacheSwitchtrue;

8. The configuration of the template engine has basically been completed, and it is very simple. Now we just need to instantiate the template engine object and we can actually run the template engine.

$TPL New // instantiate the template engine, which takes effect from the configuration before the start of this step, and does not need to pass any parameters when the template engine is instantiated. 

9. After instantiating the template engine object, we can begin to manipulate it, and who will do it? Of course is the template file, first we need to create a template file first. Created in the template file directory. The template file is actually a pure HTML code file, the extension can be customized, and we have the conventional, have. TPL is the extension. Let's say we've created a template file named: Index.tpl, because it's the same name as our PHP business logic file index.php, which is also customary by convention, because index.php file calls Index.tpl template, see name.

10. After the template file has been created, we can load the template file and inject the template variable in the business file (which was also executed in index.php), and the template variables and other template identifiers (collectively referred to as template tags) will be explained in the next steps.

// the format of the injected variable has two kinds, we pay attention to the API manual description, the array format and the traditional key-value pair format can be, we both use a bit.  $tpl//  First is the traditional key-value pair format $tpl -Assign (array//  The array format is obviously more useful, because when you inject multiple variables, you don't have to write multiple injection statements, and a word is done. If there are two identical variable names, then the previous one will be replaced. The following code, the value of the final language variable is: English.  $tpl -Assign (array(' language ' + ' Chinese ', ' language ' = ' English ')); // Next is to load the template file, directly write the template name, the template engine will automatically lock to the template file directory.  $tpl , display (' Index.tpl ');

11. Now that the template engine operation is over, we will be familiar with how each template tag in the template file is used, and what they are used for. In the prototype version, there are 9 types of template tags, namely: 1. Template variable, 2. Template constants, 3. Single-line template comments, 4. Multi-line template comments, 5. Include file load, 6. Template file loading, 7. Source template file is loaded (more special), 8. If divergence statement, 9. The Foreach Loop statement. So let's first explain the template variables.

<id= "main"    
  > 
        
  
        <href= "# # #">{$title}
  
    A  >
   < 
         href ="# # #"  > {$123} 
   
     a>div
    > 
      
    
       

12. The following is the use of template constants, template constants and PHP constants Although the name is called him constant, in fact, is not the same thing. The template constants actually look at the pseudo-constants, not the true constants, by processing the XML tags to maintain a specific set of values that need to be manually added to the constant file. (manual add is not very convenient, the author will be in the subsequent version iteration, add the function of automatic addition)

First we need to manually add the template constants in the template constants file, the code is as follows:

  XML version= "1.0" coding= "Utf-8"?><Root>    
    
     <constant>        <Key>WEBNAME 
     key>  < value > site title  
      
    
       Value > 
       
     
       constant> <  Constant><key>123abc 
         key  >  
          <value> 
         value>  c38/>   
          constant> 
           root>    
                   
      
         

Once the constants are configured, the next step is to invoke them in the template file, as follows:

  
   < title > {Webanme} 
  
    title>< p > {
   
      NAME}
   
     p>< p >
    
       {abc123}
    
      p > 
     
          

13. Here are the two types of template annotations: one for single lines and one for multiple lines. It is used to annotate the template file code, which allows the artist to understand the actual meaning of the code when it works with the design interface.

  
   
  
   
  
   
  
   
  
   
  
   
  
   
  
   
  
   
  
   

14. Template loading identifier, the loading method is divided into three categories, not divided into: include the direct loading of ordinary files; template files are compiled and loaded; source compiles the path of the compiled file directly after compiling the template file (this method is special and not perfect. Need to be used on a specific occasion, such as a call to a frame page)

The first is the normal file loading call, the code is as follows:

  
   
  
   
  
   
  
   
  
   
  
   
  
   

Here is the compilation of the template file, the code is as follows:

  
   
  
   

Finally is the template file compiled address output, the function is more special, even if not understand it does not matter, the method has a serious BUG has not been processed, and therefore not perfect, and the use of the probability is very low, here only to do a simple introduction. In subsequent versions of the iteration, whether it will be retained and refined, is still in the decision, the code is as follows:

  
   
  
   <src= "{Source path = ' Frame.tpl '}">
  
    iframe  > 

15. Next is a frequently used if divergence statement, which is similar to PHP's if statement, but is functionally simple and does not support multiple judgments and nested judgments, but I will make it progressively more powerful in subsequent versions of iterations.

   
 {if $action}
   
 <P>Interface 1 
    p>{/if}{if! $action}   
     
   <p> Interface 2 
     p>{/if}{if $action}   
      
    <P  > interface 1 
      p>{else}<P > Interface 2 
       p>{/if}{if $action}  
         
     
   
  
 

16. Finally the final and complex foreach loop is called, with the same functionality as PHP, with only a slight change in format.

 
   
   
 {foreach $array (key, value)}<P>{%key} ... {%value} 
    p>    {/foreach}{foreach $userList (ID, username)} <  
     
   P > {%id} ... {%username}  
     p> < p > {%password}  
      
     
      P > { 
       
     /foreach} 
    
       
 

Well, I can also take a long sigh of relief as I write here, and this is the end of the prototype version of the template engine. Although the teaching article is already very detailed, but it is recommended to cooperate with the API manuals and example code for reference reading, so that the effect is better and easier to understand and grasp. Of course, because it is an article, so even if you describe the details, the text is abstract, need to do more hands, and for the hands of the poor novice, this teaching article does not play much role, may instead be made confused, and even eventually disgust, so I have thought if possible later, After the version iteration has been updated to a certain extent, I will focus on a series of teaching videos, so that it will not be as abstract as text, the Novice can quickly learn.

If you are from the beginning to see the end, then I sincerely thank you for your reading, I think you should have something to say, please be sure to leave a message to tell me, there are questions please leave a message, thank you for your support!

The above describes the "PHP template Engine" Prototype prototype release! , including the aspects of the content, want to be interested in PHP tutorial friends helpful.

  • 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.