开源轻便的PHP模板引擎phptpl,简洁就是美。

phptpl是一个轻便的PHP模板引擎。不须要什么学习成本就能轻松掌握,简洁就是美。

最近想写一个项目管理平台,原来想用本身 搭建的LAPC/F平台来开发,考虑到推广使用的便捷性,最后决定重拾多年未用的PHP(不用编译就是便捷)。搜了下发现如今的PHP开发已经不是我读大 学时的原始了,模板、MVC啥啥的满天飞,PHP级别的语言用MVC仍是算了吧,模板却是个好东西,神马规模的工程都能用,最主要是分离PHP和HTML 代码,我大学时都是把PHP和HTML混杂的写,虽然直观但看的眼睛疼。其实模板实现的原理并不复杂,但网上搜到的要不都是大象级别的够我学一学期了,要 不就是太简单不少功能都没有,最后就顺手本身写了个,实际使用中效果不错,放出来给你们也玩玩 ^_^

phptpl设计目标:
·PHP模板说穿了其实就是加载一个HTML,把其中一些字符串替换后按HTML输出,好比把"$TITLE$"替换成"test phptpl"。
·网页里面不免会有大量表格,PHP模板还要处理可重复出现的明细。
·刚才看了谁实现的PHP模板引擎,拥有判断条件影响某HTML区域出现的功能,好吧,我也要支持它!

未写实现先写测试案例

PHP模板文件 test_phptpl.html php

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

测试phptpl文件 test_phptpl.php html

<?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 );
?>

天马行空一番后开始认真写phptpl实现 phptpl.php git

<?php
/**
 * 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模板引擎,输入源为字符串
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 );
}
?>

在apache里建了个虚拟主机跑test_phptpl.php,运行一次经过,看来十年前学的PHP功底仍是那么扎实,吼吼

为了展现模板处理先后对比,我再把模板HTML贴一遍 apache

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

用phptpl模板引擎处理后输出 数组

test_phptplMY_TABLE_HEADERMY_TITLE_1MY_USER_NAME_1MY_TITLE_2MY_USER_NAME_2MY_TITLE_3MY_USER_NAME_3somebody login

输出太多,整理以下 浏览器

$TITLE$

替换成了 函数

test_phptpl

学习

$TABLE_HEADER$

替换成了 测试

MY_TABLE_HEADER

ui

    $USER_ID$$USER_NAME$

替换成了

    MY_TITLE_1MY_USER_NAME_1MY_TITLE_2MY_USER_NAME_2MY_TITLE_3MY_USER_NAME_3

somebody loginno user login

过滤成了

somebody login



大功告成
最后来解释下测试代码

<?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 );
?>

函数phptpl_file第一个参数是要装载的外部HTML模板文件,第二个参数是字符串替换数组配置,赋值格式为

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

对应HTML模板里出现"(源字符串)"
这样的格式在代码中指明,也能够从配置文件读入,第三个参数和第四个参数也是用于字符串替换配置,只不过是用做两种正则表达,第五个参数是判断布尔型变量决定模板中一个区域是否出现,赋值格式为

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

对应HTML模板里

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

第六个参数是可重复明细区域的回调函数指针,赋值格式为

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

对应HTML模板里

    $USER_ID$$USER_NAME$

最后一个参数表示最后实例化的模板是否当即输出到标准输出,即浏览器,当设置为false时做为函数返回值返回到上层。

整个phptpl模板引擎只有两个函数

// 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 )

很简洁吧 是否是越看越心动了?,那就赶忙下载来玩玩吧 首页传送门 : [url]http://git.oschina.net/calvinwilliams/phptpl[/url]

相关文章
相关标签/搜索