View discuz from the PHP template engine! Template Mechanism

Source: Internet
Author: User
Tags php template forum software smarty template
This article is translated from the seventh phase of <phper> to see what people have written. In view of the familiarity with PHP and future maintenance, I recently wanted to change the ASP classified information network, and the front-end was basically revised, I used the architecture provided by fleaphp (compared with thinkphp and fleaphp, I finally chose the latter). The view used the smarty template, and the platform environment used apmserv, the development environment uses Zend development environment. The background is in progress and is planned to be completed by the end of the month.
I. Introduction to the template engine
From phplib to smarty to discuz! This article attempts to explain the author's PHP experience through the PHP template engine.
I clearly remember that when I first went to college and started learning PHP, I once saw an article in phpe.net about the performance comparison between the phplib template and fasttemplate templates. Article . Let me continue using phplib for the next six months. It is undeniable that phplib is an understanding of the PHP template engine by a generation of PHP developers on the left and right. You may be familiar with the following methods.
$ T-> set_file
$ T-> set_var
When I was not satisfied with the execution efficiency of phplib, I began to look for the next PHP template engine, so smarty jumped into my field of view, when I tried my best to learn smarty and develop a lot of things, I suddenly found that I only remember the following methods.
$ S-> assign
$ S-> display
What exactly do we need the template engine to do? MVC? Simple? Easy to use? Efficiency? See the following analysis.
II, Program Analysis of Processing
1. phplib program processing process
$ T = new template ()
$ T-> set_file
$ T-> set_var
$ T-> parse
$ T-> P
See the above Code , Which is translated into Chinese
Initialize template class $ t
Set template files
Set template Variables
Analyze template variables in the template file
Output content
At least five steps are passed to implement template processing in the PHP program.
2. Smarty program processing process
$ S = new smarty
$ S-> assign
$ S-> display
Translate to Chinese
Initialize template class $ s
Set template Variables
Parse and output templates
3. discuz! Template Process
Include template (tplname );
The main function is to specify the template file to be processed by the program.
Among the above three template processing mechanisms, discuz is the easiest to understand and accept! Template processing. Initialize and set variables, parse templates, and output content. discuz! Only one function is used. For an open-source forum software, the benefits of such processing are obvious. For discuz! The requirements of programmers who conduct secondary development are reduced. The simplified template language facilitates style and plug-in creation, which also promotes discuz to a certain extent! Spread
Iii. template Source File Syntax
When processing loop nesting in phplib, use:
<! -- Begin row -->
{It}
<! -- End row -->
When processing loop nesting in smarty
<{Section name = loopname loop = $ looparray}> (for example, foreach)
In discuz! When processing loop nesting,
<! -- {Loop $ array $ value} -->
In fact, the real template is faced with artists who do not understand PHP or know a little about PhP. The complexity of the template means that it is more difficult for the artist to create pages. When it is essential that templates have logic processing, why not use the original PHP syntax in HTML code, and let the artist learn another language? In my personal experience, it is obviously discuz! The template language is easier to learn and save more time for me.
4. discuz! Template Processing Mechanism
I strip a simple discuz! Template processing functions
Function template ($ file, $ templateid = 0, $ tpldir = ''){
$ Tplfile = discuz_root. './'. $ tpldir. '/'.20.file.'.htm'; // template source file
$ Objfile = discuz_root. './forumdata/templates/'. $ templateid. '_'. $ file. '. TPL. php'; // template cache file
// If the modification time of the template source file is later than the modification time of the template cache file,
// When the template source file is modified and the template cache is not updated,
// Call the parse_template function to regenerate the template cache file.
If (@ filemtime ($ tplfile)> @ filemtime ($ objfile )){
Require_once discuz_root. './include/template. func. php ';
Parse_template ($ file, $ templateid, $ tpldir );
}
// Return the cache file name
// $ Objfile variable content may be D: \ discuz \ forumdata \ Templates \ includemo.tpl.php
Return $ objfile;
}
The execution statement of the template on the PHP page
Include template ('Demo ');
In this example
Include 'd: \ discuz \ forumdata \ Templates \ includemo.tpl.php ';
This process is an include template ('Demo') in the demo. php file after data processing is complete to display the page.
V. Summary
I have seen many PHP template engines, but I think phplib, smarty, and discuz! The template mechanism is enough to illustrate the problem.
1. What do we need a template?
Separates programs and interfaces to facilitate program development and maintenance.
2. What are we still concerned about?
The efficiency, ease of use, and maintainability of the PHP template engine.
3. What are the final requirements?
Simple is beautiful!
The article seems to have not been written, but it has already been written. What I want to explain is to view discuz from the PHP template engine! Template mechanism. The analysis has been completed. Maybe I will write another test on the actual data for your reference! This article from PhP question Original article link: http://www.phpq.net/viewthread-28.php

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.