welcome to ...

pure-PHP templating

Hi. Here is a set of simple php scripts introdusing the idea -

" php - is a great template engine itself "

Better start looking into their source and try to guess what you'll see visiting them in your browser.

home master.php control.php top.php foot.php derived.php block.php helper.php templatelib.php multipass.php
<?
//fast-and-dirty block processing function. Processes source of <select> element and 
//marks one of its options selected depending on $value if any or $default otherwise
function selectSelector($cnt$value$default)
{
    
preg_match_all('/(<option [^>]*value="([^"]*)")([^>]*>)/'$cnt$mPREG_SET_ORDER);
    
/*    example: $m[0] ==
      array(4) {
        [0]=>   string(22) "<option value="first">"
        [1]=>   string(21) "<option value="first""
        [2]=>   string(5) "first"
        [3]=>   string(1) ">"
      }
    */
    
$curOpt = -1;
    
//try to find if some option's values matches one given in $value param
    
foreach($m as $k=>$match)
    {
        if (
$match[2]==$value)
        {
            
$curOpt $k;//remember option index and break the search
            
break;
        }
    }
    if (
$curOpt==-1)//we didn't found it, try the same for default value
    
{
        foreach(
$m as $k=>$match)
        {
            if (
$match[2]==$default)
            {
                
$curOpt $k;//remember option index and break the search
                
break;
            }
        }
    }
    if (
$curOpt==-1) return $cnt;//nothing to select here

    
$option $m[$curOpt];//<option> source
    
$cnt  str_replace($option[0], $option[1].' selected '.$option[3], $cnt);//add "selected" to it
    
return $cnt;//return changed element source
}

$GLOBALS['__rand'] = time().rand(1000099999);//random value to produce unpredictable placeholders for evaled php code
/**/
function phps($level=1)
{
    global 
$__rand;
    return 
'<%'.$level.'_'.$__rand;
}

function 
phpe($level=1)
{
    global 
$__rand;
    return 
$level.'_'.$__rand.'%>';
}

/*
searches defferred php code in the document iteration by iteration 
(first inside phps(1) phpe(1), then phps(2) phpe(2) ...)
evaluates it and replaces the code with the returned value
Note: the local variables are double-underscored here, as we do extract()
and do not need the collisions
@param string $__cnt content to process
@param mixed $__env global scope of the php page
*/
function multipass($__cnt$__env)
{
    global 
$__rand;
    
extract($__envEXTR_PREFIX_SAME'ext');    
    
$__level 1;
    
//$__first = true;//first search at the curent $__level    
    
do
    {
        
$__cnt str_replace(array('<?''?>''<?php'), array('-!phpopen!-''-!phpclose!-''-!phplongopen!-'), $__cnt);
        
$__cnt preg_replace('/<script [^>]*?language\s*?=\s*?((?:"|\')?)php\1/is''-!phpscriptopen!-'$__cnt);
        
$__open phps($__level);
        
$__close phpe($__level);
        
$__regExp '/'.$__open.'(.*?)'.$__close.'/s';
        
$__found preg_match($__regExp$__cnt$__m);
        
        if (!
$__found)
        {
            
$__cnt str_replace(array('-!phpopen!-''-!phpclose!-''-!phplongopen!-''-!phpscriptopen!-'), array('<?''?>''<?php''<script language="php"'), $__cnt);
            
            return 
$__cnt;
        }
        
        
$__cnt str_replace(array($__open$__close), array('<?''?>'), $__cnt);
        
$__fname dirname(__FILE__).'/tmp'.$__rand.'.php';
        
file_put_contents($__fname$__cnt);
        
ob_start();
        include(
$__fname);
        
$__cnt ob_get_clean();        
        
unlink($__fname);
        
$__level++;
    } while(
true);
}

$GLOBALS['__option']=null;
$GLOBALS['__defOption']=null;
/*draws opening <select> tag and optionally its options*/
function select($attributes=''$selected=null$default=null$options=null)
{
    global 
$__option$__defOption;    
    
$__option $selected;
    
$__defOption $default;
    
$r '<select';
    if (
$attributes)
    {
        if(
strpos($attributes'=')!==false)
        {
            
$r .= ' '.$attributes;
        }
        else
//it is just name
        
{
            
$r .= ' name="'.htmlspecialchars($attributes).'"';
        }
    }
    
$r .= '>'."\n";        
    echo 
$r;    
    if (
is_array($options))
    {
        foreach(
$options as $key=>$display)//value=>display
        
{
            if (
is_numeric($key))
            {
                
option($display);
            }
            else
            {
                
option($display''$key);
            }
        }
    }    
}

/*draws opening <option> tag. If it will be selected depends on its value 
and params passed to select()*/
function option($displayValue$value=null$attributes='')
{
    global 
$__option$__defOption;    
    
$r '<option';
    if (
$attributes)
    {
        
$r .= ' '.$attributes;
    }
    if (
$value===null$value=$displayValue;
    
    
$r .=' value="'.htmlspecialchars($value).'"';
    
$val $__option?$__option:$__defOption;
    if (
$value==$val$r .= ' selected';
    
$r .= '>'.$displayValue.'</option>'."\n";    
    echo 
$r;
}