Phptpl is a simple PHP template engine. Easy to grasp without any learning costs. simplicity is beautiful.

Source: Internet
Author: User
Tags php template
Phptpl is a lightweight PHP template engine. It is easy to grasp without any learning costs. simplicity is beautiful. Phptpl is a lightweight PHP template engine. It is easy to grasp without any learning costs. simplicity is beautiful. Recently I want to write a project management platform. I originally wanted to use my own LAPC/F platform for development, considering that phptpl is a lightweight PHP template engine. It is easy to grasp without any learning costs. simplicity is beautiful.
Phptpl is a lightweight PHP template engine. It is easy to grasp without any learning costs. simplicity is beautiful.

Recently I want to write a project management platform. I originally wanted to use my own LAPC/F platform for development. Considering the convenience of promotion and use, finally, I decided to refresh the unused PHP for many years (without compilation, it would be convenient ). After searching, I found that the current PHP development is no longer the original one when I was in college. the templates, MVC, and other things are full of sky, and I still forget to use MVC for PHP-level languages, the template is a good thing. it can be used in projects of Shenma scale. The most important thing is to separate PHP and HTML code. when I was a college student, I wrote a mix of PHP and HTML, although intuitive but eye pain. In fact, the implementation principle of templates is not complicated, but I have not found the elephant level on the Internet enough for a semester, or it is too simple and has no functions, finally, I wrote it by myself. it worked well in actual use. let's play with it. ^_^

Phptpl design goals:
· The PHP template simply loads an HTML file and replaces some of the strings with HTML, for example"$ TITLE $"Replace"Test phptpl".
· There will inevitably be a large number of tables on the webpage, and the PHP template also needs to process repeated details.
· I have just read who implemented the PHP template engine and have the ability to determine whether conditions affect the appearance of an HTML region. well, I also want to support it!

Write test cases before implementation

PHP template file test_phptpl.html
[Code]





$ TITLE $


$ TABLE_HEADER $




$ USER_ID $


$ USER_NAME $





Somebody login



No user login





[/Code]

Test the phptpl file test_phptpl.php
[Code]
/**
* Test phptpl
*/

Require "phptpl. php ";

// String replacement configuration
$ Str_replace_array ['$ TITLE $'] = "test_phptpl ";
$ Str_replace_array ['$ TABLE_HEADER $'] = "MY_TABLE_HEADER ";
// Detailed replacement configuration
Function detail_function ($ in_str = null, $ print_flag = false)
{
If ($ in_str = null)
Return null;

$ Out_str = "";
For ($ I = 1; $ I <= 3; $ I ++)
{
$ Str_replace_array = array ();
$ Str_replace_array ['$ USER_ID $'] = "MY_TITLE _". (string) $ I;
$ Str_replace_array ['$ USER_NAME $'] = "MY_USER_NAME _". (string) $ I;

$ Out_str. = phptpl_str ($ in_str, $ str_replace_array, null, null, false );
}

If ($ print_flag = true)
Print $ out_str;

Return $ out_str;
}
$ Section_replace_array ['$ DETAIL $'] = "detail_function ";
// Configuration exists in the region
$ If_exist_array ['$ LOGIN $'] = true;
// Execute template processing and output immediately
Phptpl_file ("test_phptpl.html", $ str_replace_array, null, null, $ if_exist_array, $ section_replace_array, true );
?>
[/Code]

After a while, I began to carefully write phptpl to implement phptpl. php.
[Code]
/**
* Phptpl-PHP Cute Template Engine
* AUTHOR: calvin (calvinwilliams.c@gmail.com)
* COPYRIGHT: by calvin
* LICENSE: LGPL (http://www.gnu.org/licenses/lgpl.html)
* VERSION: v1.0.0 2014-02-16 create
*/

// PHP template engine. the input source is a string
Function phptpl_str ($ in_str = null, $ str_replace_array = null, $ response = null, $ preg_replace_array = null, $ if_exist_array = null, $ section_replace_array = null, $ print_flag = false)
{
...
}
?>
[/Code]

I created a virtual host in apache to run test_phptpl.php and run it once. it seems that the PHP skills I learned a decade ago are still so solid.

To display the comparison before and after template processing, I will paste the template HTML again.
[Code]





$ TITLE $


$ TABLE_HEADER $




$ USER_ID $


$ USER_NAME $





Somebody login



No user login





[/Code]

Output after processing with phptpl template engine
[Code]

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.