Open source and lightweight PHP template engine phptpl, simplicity is beauty.

phptpl is a lightweight PHP template engine. It can be easily mastered without any learning cost, and simplicity is beauty.

Recently, I wanted to write a project management platform. I originally wanted to use the LAPC/F platform I built for development. Considering the convenience of promotion and use, I finally decided to pick up PHP, which has not been used for many years (no need to compile is convenient). I searched and found that the current PHP development is not the original when I was in college. Templates, MVC, etc. are flying all over the sky. For PHP-level languages, MVC is still a good thing. Templates are a good thing. The most important thing is to separate PHP and HTML code. When I was in college, I mixed PHP and HTML. Although it is intuitive, it hurts my eyes. In fact, the principle of template implementation is not complicated, but what I found on the Internet is either elephant-level enough for me to study for a semester, or it is too simple and does not have many functions. In the end, I wrote it myself. In actual use The effect is good, let it out for everyone to play with ^_^

The design goal of phptpl:
·The PHP template, to put it bluntly, is actually to load an HTML, replace some of the strings and then press the HTML output, for example, replace "$TITLE$" with "test" phptpl".
·There will inevitably be a lot of tables in the web page, and the PHP template has to deal with repeatable details.
·I just saw who implemented the PHP template engine. It has the function of judging conditions affecting the appearance of a certain HTML area. Well, I also support it! Write the test case PHP template file test_phptpl.html

before writing the implementation

$TITLE$$TABLE_HEADER$$USER_ID$$USER_NAME$somebody loginno user login

Test the phptpl file test_phptpl.php

<?php
/**
 * test phptpl
 */

require "phptpl.php" ;

// 字符串替换配置
$str_replace_array['$TITLE$'] = "test_phptpl" ;
$str_replace_array['$TABLE_HEADER$'] = "MY_TABLE_HEADER" ;
// 明细替换配置
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" ;
// 区域存在配置
$if_exist_array['$LOGIN$'] = true ;
// 执行模板处理并立即输出
phptpl_file( "test_phptpl.html" , $str_replace_array , null , null , $if_exist_array , $section_replace_array , true );
?>

After a lot of imagination, I started to write phptpl seriously to realize phptpl.php

<?php
/**
 * 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模板引擎,输入源为字符串
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 )
{
    if( $in_str == null )
        return null;
    
    $out_str = $in_str ;
    
    // 处理字符串替换
    if( $str_replace_array != null )
    {
        $replace_from_array = array() ;
        $replace_to_array = array() ;
        
        foreach( $str_replace_array as $key => $value )
        {
            $value = $str_replace_array[$key] ;
            
            $replace_from_array[] = $key ;
            $replace_to_array[] = $value ;
        }
        
        $out_str = str_replace( $replace_from_array , $replace_to_array , $out_str ) ;
    }
    
    // 处理字符串PCRE正则替换
    if( $ereg_replace_array != null )
    {
        $replace_from_array = array() ;
        $replace_to_array = array() ;
        
        foreach( $ereg_replace_array as $key => $value )
        {
            $value = $ereg_replace_array[$key] ;
            
            $replace_from_array[] = $key ;
            $replace_to_array[] = $value ;
        }
        
        $out_str = ereg_replace( $replace_from_array , $replace_to_array , $out_str ) ;
    }
    
    // 处理字符串POSIX正则替换
    if( $preg_replace_array != null )
    {
        $replace_from_array = array() ;
        $replace_to_array = array() ;
        
        foreach( $preg_replace_array as $key => $value )
        {
            $value = $preg_replace_array[$key] ;
            
            $replace_from_array[] = $key ;
            $replace_to_array[] = $value ;
        }
        
        $out_str = preg_replace( $replace_from_array , $replace_to_array , $out_str ) ;
    }
    
    // 处理区域存在
    if( $if_exist_array != null )
    {
        foreach( $if_exist_array as $key => $value )
        {
            $begin_str = "" ;
            $middle_str = "" ;
            $end_str = "" ;
            $begin_pos = strpos( $out_str , $begin_str ) ;
            $middle_pos = strpos( $out_str , $middle_str ) ;
            $end_pos = strpos( $out_str , $end_str ) ;
            if( $begin_pos == true && $end_pos == true )
            {
                if( $middle_pos == false )
                {
                    if( $value == false )
                    {
                        $segment_str1 = substr( $out_str , 0 , $begin_pos ) ;
                        $segment_str3 = substr( $out_str , $end_pos + strlen($end_str) ) ;
                        $out_str = $segment_str1 . $segment_str3 ;
                    }
                }
                else
                {
                    if( $value == true )
                    {
                        $segment_str1 = substr( $out_str , 0 , $middle_pos ) ;
                        $segment_str3 = substr( $out_str , $end_pos + strlen($end_str) ) ;
                        $out_str = $segment_str1 . $segment_str3 ;
                    }
                    else
                    {
                        $segment_str1 = substr( $out_str , 0 , $begin_pos ) ;
                        $segment_str3 = substr( $out_str , $middle_pos + strlen($middle_str) ) ;
                        $out_str = $segment_str1 . $segment_str3 ;
                    }
                }
            }
        }
    }
    
    // 处理明细
    if( $section_replace_array != null )
    {
        foreach( $section_replace_array as $key => $value )
        {
            $begin_str = "" ;
            $end_str = "" ;
            $begin_pos = strpos( $out_str , $begin_str ) ;
            $end_pos = strpos( $out_str , $end_str ) ;
            if( $begin_pos == true && $end_pos == true )
            {
                $segment_str1 = substr( $out_str , 0 , $begin_pos ) ;
                $segment_str2 = substr( $out_str , $begin_pos + strlen($begin_str) , $end_pos - $begin_pos - strlen($begin_str) ) ;
                $segment_str3 = substr( $out_str , $end_pos + strlen($end_str) ) ;
                
                $segment_str2 = $section_replace_array[$key]( $segment_str2 , false ) ;
                $out_str = $segment_str1 . $segment_str2 . $segment_str3 ;
            }
        }
    }
    
    /* 处理立即输出标志 */
    if( $print_flag == true )
        print $out_str;
    
    return $out_str;
}

// PHP模板引擎,输入源为模板文件名
function phptpl_file( $in_pathfilename = null , $str_replace_array = null , $ereg_replace_array = null , $preg_replace_array = null , $if_exist_array = null , $section_replace_array = null , $print_flag = false )
{
    if( $in_pathfilename == null )
        return null;
    
    $fp = fopen( $in_pathfilename , "r" ) ;
    $out_str = fread( $fp , filesize($in_pathfilename) );
    fclose($fp);
    
    return phptpl_str( $out_str , $str_replace_array , $ereg_replace_array , $preg_replace_array , $if_exist_array , $section_replace_array , $print_flag );
}
?>

I built a virtual host in apache to run test_phptpl.php, run it once, and it seems that the PHP skills I learned ten years ago are still solid.

In order to show the comparison before and after the template processing, I will paste the template HTML again.

$TITLE$$TABLE_HEADER$$USER_ID$$USER_NAME$somebody loginno user login

Output after processing with phptpl template engine

test_phptplMY_TABLE_HEADERMY_TITLE_1MY_USER_NAME_1MY_TITLE_2MY_USER_NAME_2MY_TITLE_3MY_USER_NAME_3somebody login

Too much output, organized as follows

$TITLE$

replaced by

test_phptpl

span

$TABLE_HEADER$

replaced by

MY_TABLE_HEADER

span

    $USER_ID$$USER_NAME$

replaced by

    MY_TITLE_1MY_USER_NAME_1MY_TITLE_2MY_USER_NAME_2MY_TITLE_3MY_USER_NAME_3

span

somebody loginno user login

filtered into

somebody login



Okay, you're done .
Finally, let's explain the test code.

<?php
/**
 * test phptpl
 */

require "phptpl.php" ;

// 字符串替换配置
$str_replace_array['$TITLE$'] = "test_phptpl" ;
$str_replace_array['$TABLE_HEADER$'] = "MY_TABLE_HEADER" ;
// 明细替换配置
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" ;
// 区域存在配置
$if_exist_array['$LOGIN$'] = true ;
// 执行模板处理并立即输出
phptpl_file( "test_phptpl.html" , $str_replace_array , null , null , $if_exist_array , $section_replace_array , true );
?>

The first parameter of the function phptpl_file is the external HTML template file to be loaded, the second parameter is the string replacement array configuration, and the assignment format is

$str_replace_array['(源字符串)'] = "(目标字符串)" ;

The format of "(source string)" in the corresponding HTML template
is indicated in the code, and it can also be read from the configuration file. The third and fourth parameters are also used for string replacement configuration, but they are used as Two regular expressions, the fifth parameter is to judge whether a boolean variable determines whether an area in the template appears, and the assignment format is

$if_exist_array['(区域名)'] = true ;

In the corresponding HTML template

<!-- IF (区域名) -->
区域1
<!-- ELSE (区域名) -->
区域2
<!-- ENDIF (区域名) -->

The sixth parameter is the callback function pointer of the repeatable detail area, and the assignment format is

$section_replace_array['$DETAIL$'] = "detail_function" ;

In the corresponding HTML template

    $USER_ID$$USER_NAME$

The last parameter indicates whether the last instantiated template is immediately output to the standard output, that is, the browser. When set to false, it is returned to the upper layer as a function return value.

The entire phptpl template engine has only two functions

// PHP模板引擎,输入源为字符串
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 )
// PHP模板引擎,输入源为模板文件名
function phptpl_file( $in_pathfilename = null , $str_replace_array = null , $ereg_replace_array = null , $preg_replace_array = null , $if_exist_array = null , $section_replace_array = null , $print_flag = false )

It's very simple

, isn't it more exciting? , then download and play
. Home Portal: [url]http://git.oschina.net/calvinwilliams/phptpl[/url]

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324081219&siteId=291194637