PHPTPL is a simple php template engine. No need to learn the cost can be easily mastered, concise is the United States

Source: Internet
Author: User
Tags php template
PHPTPL is a lightweight php template engine. No need to learn the cost can be easily mastered, concise is the United States.
PHPTPL is a lightweight php template engine. No need to learn the cost can be easily mastered, concise is the United States.

Recently wanted to write a project management platform, originally wanted to use their own lapc/f platform to develop, considering the promotion of the use of convenience, finally decided to regain years of unused PHP (without compiling is convenient). Search the discovery of the current PHP development is not my original college, template, MVC What is flying, PHP-level language with MVC or forget it, the template is a good thing, God horse scale works can be used, the most important is to separate PHP and HTML code, When I was in college, I wrote a mix of PHP and HTML, though intuitive eyes hurt. In fact, the principle of the implementation of the template is not complex, but the search on the internet is not an elephant level enough I learned a semester, or is too simple a lot of functions are not, and finally handy to write a, the actual use of the effect is good, put out to everyone also play ^_^

PHPTPL Design Goals:
· The PHP template is actually loading an HTML, replacing some of the strings with HTML output, such as "$TITLE $"Replace"Test PHPTPL"。
• There will inevitably be a large number of forms in the Web page, and the PHP template will also handle repeatable details.
• Just looked at who implemented the PHP template engine, has the ability to judge the condition that affects an HTML area, OK, I also want to support it!

Write a test case without write implementation

PHP Template Files test_phptpl.html
[Code]





<title>$TITLE $</title>


$TABLE _header$








$USER _id$

$USER _name$


Somebody login



No user Login





[/code]

Test PHPTPL file test_phptpl.php
[Code]
/**
* Test PHPTPL
*/

Require "phptpl.php";

String substitution Configuration
$str _replace_array[' $TITLE $ '] = "TEST_PHPTPL";
$str _replace_array[' $TABLE _header$ '] = "My_table_header";
Detail 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, NULL, FALSE);
}

if ($print _flag = = True)
Print $out _str;

return $out _str;
}
$section _replace_array[' $DETAIL $ '] = "detail_function";
Zone Presence Configuration
$if _exist_array[' $LOGIN $ '] = true;
Perform 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 few phptpl began to earnestly write the implementation of phptpl.php
[Code]
/**
* phptpl-php Cute Template Engine
* Author:calvin ([email protected])
* Copyright:by Calvin
* LICENSE:LGPL (http://www.gnu.org/licenses/lgpl.html)
* version:v1.0.0 2014-02-16 Create
*/

PHP template engine, input source as String
function Phptpl_str ($in _str = null, $str _replace_array = null, $ereg _replace_array = null, $preg _replace_array = null , $if _exist_array = null, $section _replace_array = null, $print _flag = False)
{
...
}
?>
[/code]

In Apache built a virtual host run test_phptpl.php, run a pass, it seems ten years ago learned PHP Foundation is still so solid, roar

In order to demonstrate the template processing before and after the comparison, I put the template HTML again
[Code]





<title>$TITLE $</title>


$TABLE _header$








$USER _id$

$USER _name$


Somebody login



No user Login





[/code]

Post-processing output 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.