Manually developing PHP template engine 1 (35)

Source: Internet
Author: User
Tags php template smarty template
Manually developing PHP template engine one (35)

The template is called a TPL and mimics the Smarty template engine.

The template we're talking about is a Web template, a page written primarily by a language composed of HTML markup, but there's also a way to represent a dynamic generation of content (parsing tags). The template engine is a software library that allows us to
Generate HTML code from the template and specify the dynamic content to include.

1 features of the template engine:

1. Encouragement of separation: improved readability and maintainability of the system.
2. Promote the division of labor: so that programmers and artists to concentrate on their own design.
3. Easier to parse than PHP: Compile files and cache files load faster and take up less resources.

4. Increased security: can limit the ability of the template designer to do unsafe operation to avoid accidental deletion of erroneous access and so on.

2 Template Engine Products:

PHP has a team of specially developed template engines, such as Smarty, Heyes Templates Class,
Fasttemplate and so on. These template engines we use directly, we can fully realize the above many
Characteristics. But for beginners, understanding the principles of the template engine can be a more profound understanding of why the use of modulo
Plate.

3 TPL Template flowchart

When we create the template engine ourselves, the greatest benefit is simplicity. Because many teams write a good template engine, its functionality is very powerful, and security is high. But the disadvantage is that many of us can't use it, and the volume is very

of the bloated.

  

4. Create a TPL template engine

1. The folders and files required to create the initial template

A) index.php the master file for writing business logic.
b) template.inc.php template initialization file for initial templates information.
c) The templates directory holds all template files.
d) The Templates_c directory holds all compiled files.
e) The cache directory holds all cached files.
f) The includes directory holds all class files.
g) The Config directory holds the template system variable configuration file.

2. index.php file code in the root directory of the Web site

//set encoding to Utf-8Header'Content-type:text/html;charset=utf-8');//Site root directoryDefine'Root_path', DirName (__file__));//Store Templates folderDefine'Tpl_dir', Root_path.'/templates/');//Compiling foldersDefine'Tpl_c_dir', Root_path.'/templates_c/');//Cache FolderDefine'Cache_dir', Root_path.'/cache/');

3 includes folder templates.class.php-Add a method to determine if a directory exists

// Create a constructor method  Public function __construct () {// Verify if the directory exists  if (!is_dir (tpl_dir) | | |!is_dir (TPL_C_DIR) | | ! Is_dir (Cache_dir)) {exit ('ERROR: Template folder or compilation folder or cache folder not created!  '); }}  

4 includes folder under the templates.class.php-Create display () method to load the. TPL template file

This is also the display prototype method inside the Samrty.

//import a template into a php file     Publicfunction Display ($_file) {//set the path to the template file$_tplfile =Tpl_dir.$_file;//determine if a template file exists    if(!file_exists ($_tplfile)) {Exit ('ERROR: Template file does not exist! '); }//set the file name of the compiled files$_parfile = Tpl_c_dir.md5 ($_file). $_file.'. PHP';//determine if the compiled file exists and the template file has been modified    if(!file_exists ($_parfile) | | filemtime ($_PARFILE) <filemtime ($_tplfile)) {//generate a compiled filefile_put_contents ($_parfile,file_get_contents ($_tplfile)); }//loading the compiled fileinclude $_parfile;}//Introducing Template ClassesRequire Root_path.'/includes/template.class.php';//instantiating a template class$_TPL =NewTemplate ();//Load Index.tpl$_tpl->display ('index.tpl);

Not to be continued

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