[PHP template engine] Prototype is released !, Prototype _ PHP Tutorial

Source: Internet
Author: User
Tags api manual php oop php template
[PHP template engine] Prototype is released !, Prototype. [PHP template engine] Prototype is released !, The prototype is at the beginning of the article. First, you must say sorry to those who have been paying attention to me! Because it was originally intended to publish the Prototype of the [PHP template engine] in the frontend Framework 5.0 !, Prototype

At the beginning of the article, you must first say sorry to those who have been paying attention to me! Because it was originally intended to complete the initial version of the PHP template engine immediately after the frontend Framework 5.0 was released. But I failed to do it, and it took me 15 years to finish New Year's day. I am so sorry for the serious procrastination!

I have mentioned earlier that the relevant API usage instructions will be published in subsequent works, but I think this is not good enough and the blog platform is not very friendly in processing and displaying tables, as a result, the API cannot be perfectly presented. Therefore, we plan to provide only the API link. you can visit our official website to check the manual through the link. this is the best reading experience. The published articles will update some API-related usage instructions and provide some small examples so that you can read the API at the same time, we can also use practical examples to better understand various code usage and business logic ideas!

The following is the link between the API manual and the demo.

[Demo]: http://www.shibuyi.net/demo/php/template_engine/prototype

[API user manual]: http://www.shibuyi.net/api/php/template_engine/prototype

[My personal website]: http://www.shibuyi.net

The following describes how to use the Prototype of the PHP template engine!

What? I don't know what the template engine is? Some new PHP users may ask such a question. let me give a simple answer. The template engine is actually a middleware technology of PHP, making programming for traditional Web dynamic websites easier. why? Was it not easy in the past? That's because before the appearance of the template engine, almost all Web server programmers completed their work through the mixed code. The so-called mixed code is a page that contains: PHP also contains HTML and even code in other languages. Therefore, if the project is gradually expanded, the development and maintenance time is very high and error-prone, and the combination of the artist and program is not good, if the artist does not understand the program at all, there is basically no way to read the mixed-editing files. The birth of the template engine solves this problem well. It uses the template engine technology to cut and separate the PHP business logic-layer code from the presentation-Layer Code HTML in mixed-editing files. programmers can design programs with peace of mind, the artist designed the interface with peace of mind, and the two will be associated with a special symbol marked by the template, so that the template engine can smoothly compile the template file after reading the template file. PHP on the market already has many mature template engines, such as the Smarty template engine. other Web server languages also have their own template engines, or middleware technologies similar to the template engine.

About the template engine in detail, we recommend that you visit Baidu Encyclopedia: http://baike.baidu.com/view/4258079.htm

Before learning the template engine, you must first ensure that you have understood the idea of php oop (object-oriented programming), because the template technology is described using the idea of OOP, if you do not understand OOP, this article is not suitable for you to read, because the threshold is too high!

First, we can see the template engine directory structure (the author uses ZendStudio 7.2.1 Integrated Development Environment ):

1. caches is the template cache Directory (if the template engine does not exist, it will be automatically generated during the first execution );

2. classes is the core class library of the template engine;

3. compiles is the template compilation Directory (if the template engine does not exist, it will be automatically generated during the first execution );

4. constants is the template constant Directory (if the template engine does not exist, it will be automatically generated during the first execution );

5. configure the Directory for the template by using DES;

6. templates is the template file directory (if the template engine does not exist, it will be automatically generated during the first execution ).

After learning about the template engine Directory, let's take a look at how to make it work. In the prototype version, the template engine initialization and configuration information are all completed in the DES/template. inc. php file (you can open the source code for reference ).

In fact, the template engine configuration is also called the initialization process. The first step of initialization is to configure the corresponding directory, enable the template engine to correctly read and write data in this directory (you can choose the configuration steps, not necessarily in my order, but it must be completed before the template engine is instantiated, otherwise it will become invalid), and we assume that it is configured in the same directory as the template engine, if the root directory is not the same, you need to adjust the configuration.

1. the first step is to configure the template engine root directory. If this parameter is not set, the absolute path of the root directory is automatically generated.

Template: $ rootPath = dirname (_ FILE _); // both relative and absolute paths are acceptable. here we use absolute paths! For example: "C:/wwwroot/prototype"

2. configure the template file directory, which is used to store the template file. if not set, the default directory is templates.

// The default directory is used here. you can set it based on your needs. it is recommended that you do not write Chinese characters, which is prone to errors. it doesn't matter if you add a forward or backward backslash to the directory name, the final template engine is automatically corrected! Template: $ templateName = '/templates /';

3. configure the compiling file directory, which is used to exist the compiled file generated after the template file is parsed. if not set, the default value is: compiles directory.

Template: $ compileName = '/compiles/'; // use the default directory like the Template directory.

4. configure the cache file directory. this is the cache file generated by the compiled file after the template engine enables the cache function. if it is not set, the default value is the caches directory.

Template: $ cacheName = '/caches/'; // use the default

5. if you configure the template constant directory, you may not understand what the template constant is for. what is the difference between it and common PHP constants? For the template constant explanation, we will discuss it in detail in the next step. here we will configure it with me first. if it is not set to the constants directory by default.

Template: $ constantName = '/constants/'; // use the default

6. By the end of step 6, the directory configuration has been completed. you do not have to worry about the problem that the directory does not exist or manually create the directory. the template engine automatically completes the configuration. Set the file name of the template constant. If this parameter is not set, the default value is the default. xml file.

// We also use the default value, but you must note that the default value must be used here. xml is the extension, because the constant file is marked with XML, if not. the end of xml may cause an exception when the template engine processes constants! Template: $ constantFile = 'default. XML ';

7. set the cache switch. the cache is disabled by default. it is enabled only when we set it.

// Note that here I write a Boolean value. In fact, you can enter any value here, which is converted to a Boolean value by implicit conversion. you can write either 0 or 1, I directly write a Boolean value for your convenience! Template: $ cacheSwitch = true;

8. now the template engine configuration is basically complete, and it is still very simple. Now we only need to create a template engine object to run the template engine.

$ Tpl = new Template (); // instance-based Template engine. all the configurations before this step take effect. no parameters need to be passed during Template engine instantiation.

9. after the template engine object is created for an instance, we can start to operate on it. who will perform the operation on? Of course it is a template file. First, we need to create a template file. Create a file in the template file directory. The template file is actually a pure HTML code file, and the extension can be customized. In general, the extension is. tpl. Suppose we have created a template named index. tpl, because it is different from our php business logic file index. php has the same name. this is also customary because of index. the php file calls index. tpl template, known and known.

10. after the template file is created, we can go to the business file (the previous configuration is also in index. load the template file and inject the Template variables in php. The Template variables and other template identifiers (collectively referred to as template tags) will be explained one by one in the following steps.

// There are two formats for injecting Variables. you can see the instructions in the API manual. Both the array format and the traditional key-value pair format can be used. $ Tpl-> assign ('title', 'title '); // the format of the traditional key-value pair $ tpl-> assign (array ('title' => 'title', 'name' => 'name ')); // the array format is obviously easier to use, because when multiple variables are injected, you don't need to write multiple injection statements, just one sentence. // If two identical variable names are displayed, the previous variable names will be replaced later. In the following code, the final language variable value is: English. $ Tpl-> assign (array ('language' => 'Chinese', 'language' => 'INC'); // load the template file, directly write the template name. The template engine will automatically lock the template file directory. $ Tpl-> display ('index. tpl ');

11. now the operation on the template engine is complete. next we will familiarize ourselves with the usage of the template tags in the template file. what are they used. In the prototype, there are nine types of template tags: 1. template variable, 2. template constant, 3. single Row template comment, 4. multi-line template comment, 5. include File loading, 6. template file loading, 7. source template file loading (special), 8. if statement, 9. foreach loop statement. First, let's explain the Template variables.

{$ Title} {$123}

12. The following is the use of template constants. although both the template constants and PHP constants are called their constants, they are essentially not the same thing. A template constant is actually a pseudo constant rather than a real constant. it maintains a specific set of unchanged values by processing XML tags, these values must be manually added to the constant file. (Manual addition is not very convenient. I will add the automatic addition function in subsequent version iterations)

First, manually add template constants in the template constant file. the code is as follows:

Website title

After the constant is configured, call it in the template file. the code is as follows:




13. The following is the template annotator. There are two types: single row and multi-row. It is mostly used to annotate the template file code, so that the artist can understand the actual meaning of the code in concert with the design interface.

 {@ Normal single row comment}
 {@ Line feed single line comment}
 {#} This is a multi-line comment. pay attention to the first echo! {/#}
 {#} This is a multi-line comment. I have wrapped it! {/#}
 {#} Does not have an ending sign.

14. template loading identifiers, which can be divided into three types: include: direct loading of common files; template: load template files after compilation; source directly outputs the path of the compiled file after compiling the template file (this method is special and not perfect and needs to be used in a specific scenario, such as calling the Framework page)

The first is to load and call common files. the code is as follows:

 {Include path = "test. php "}
 {Include path = 'ABC. php "}
 {Include path = "123.php "}
 {Include path = "123.php "}

The following is the compilation and loading of the template file. the code is as follows:

 {Template path = 'Test. tpl '}

Finally, it is the output of the compilation address of the template file. This function is special. even if you don't understand it, it doesn't matter. this method has a serious BUG that has not been completed, so it is not perfect, the probability of use is also very low. here is a brief introduction. In subsequent version iterations, whether or not it will be retained and improved is still being determined. the code is as follows:


15. next, we will often use different if statements. they are similar to php's if statements, but they are very simple in function and do not support multiple judgments and nested judgments, however, I will make it more powerful in later version iterations.

 {If $ action}

Interface 1

{/If} {if! $ Action}

Interface 2

{/If} {if $ action}

Interface 1


Interface 2

{/If} {if $ action}

16. at last, the complex foreach loop statement was called. its function is the same as that of PHP, but the format is slightly changed.

 {Foreach $ array (key, value )}

{% Key}... {% value}

{/Foreach} {foreach $ userList (id, username )}

{% Id}... {% username}

{% Password}


Now, I can write it here to make it easy. the guidance of the template engine prototype is over. Although the teaching article has been very detailed, it is recommended that you refer to the API manual and instance code for better understanding and understanding. Of course, because it is an article, even if you describe it in detail, the text is abstract and requires more hands-on work. for beginners with poor hands-on capabilities, this teaching article does not play much role, but may be confused, or even dislike, so I have thought that if it is possible in the future, after the version iterations are updated to a certain extent, I will produce a targeted series of teaching videos, so that they will not be abstracted like text, and new users will be able to learn quickly.

If you see it from the beginning and end, I sincerely thank you for reading it. I think you have something to say. please leave a message below to tell me. if you have any questions, please leave a message in time, thank you for your support ~!

Middleware template engine: Prototype is released !, The prototype is at the beginning of the article. First, you must say sorry to those who have been paying attention to me! Because it was originally intended to be released in front-end Framework 5.0...

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.