<?php
/*******************************************************************************
 *                   dougk_ff7's quick and dirty template factory              *
 *                    Copyright (C) 2003 Doug Kelly (dougk_ff7)                *
 *        functions:  load_template(), parse_template(), parse_block(),        *
 *                                 show_template()                             *
 *******************************************************************************/
 
/*******************************************************************************
 * function: load_template()                                                   *
 *   parameters: template [string], filename [string]                          *
 *   returns: template [array] - contents of file                              *
 *   extra: none, that I know of....                                           *
 *******************************************************************************/

function load_template($template_name$filename)
{
    
$template file('./template/' $template_name '/' $filename);
    if ( !
$template )
    {
        die(
"Could not load template: name: $template_name file: $filename");
    }
    return 
$template;
}

/*******************************************************************************
 * function: parse_template()                                                  *
 *   parameters: template [string], variables [array]                          *
 *   returns: template [array] - contents of file                              *
 *   extra: none, that I know of....                                           *
 *******************************************************************************/
function parse_template($template$vars=array())
{
    for(
$i=0$i count($template); $i++)
    {
        if( 
preg_match('/<!--BEGIN (.*?) -->/'$template[$i]) )
        {
            
// ok, we found a begin block... let's skip it for now.
            
while($tmp preg_match('/<!--END (.*?) -->/'$template[$i]))
            {
                if (
$tmp == TRUE)
                {
                    
// we succeeded in finding the end, stop.
                    
break;
                }
                if(
$i count($template) )
                {
                    
// oops, no end block, let's break out of this.
                     
break;
                }
                
$i++;
            }
        }
        
$keys array_keys($vars);
        for(
$j=0$j<count($keys); $j++)
        {
            
$keys[$j] = '/\{' $keys[$j] . '\}/';
        }
        
$template[$i] = preg_replace($keys$vars$template[$i]);
    }    
    return 
$template;    
}

/*******************************************************************************
 * function: parse_block()                                                     *
 *   parameters: template [string], block [string], variables [array]          *
 *   returns: template [array] - contents of file                              *
 *   extra: a very strange implementation... any suggestions?                  *
 *******************************************************************************/
function parse_block($template$block$vars=array())
{
    for(
$i=0$i count($template); $i++)
    {
        if( 
preg_match('/<!--BEGIN ' $block ' -->/'$template[$i]) )
        {
            
// ok, found the block we're looking for... let's find and replace the variables
            
$begin $i+1;
        }
        if( 
preg_match('/<!--END ' $block ' -->/'$template[$i]) )
        {
            
// we're at the end, break.
            
$end $i-1;
        }
    }
    
    for(
$i=$begin$i<($end+1); $i++)
    {
        for(
$j=0$j<count($vars); $j++)
        {
            
//
            // first, we'll grab the keys and place them in an array, with regex style.
            //
            
$keys array_keys($vars[$j]);
            for(
$k=0$k<count($keys); $k++)
            {
                
$keys[$k] = '/\{' $block '.' $keys[$k] . '\}/';
            }
            
//
            // and now replace that block.
            //
            
$tmp[$j][$i] = preg_replace($keys$vars[$j], $template[$i]);
        }
    }    

    
//
    // we're going to append to one line in a bit, so clear it out first/
    //
    
$template[$begin] = '';
    for(
$i=0$i<count($vars); $i++)
    {
        
// we want this to fit - flatten it. (no sense in adding extra line breaks)
        
$template[$begin] .= implode(''$tmp[$i]);
    }
    
    for(
$i=($begin+1); $i<($end+1); $i++)
    {
        
//
        // set all other lines we didn't use to ''
        //
        
$template[$i] = '';
    }
    
    
//
    // correct for the little addition we did above (removes <!-- BEGIN --> and <!-- END -->
    // Not useful, not terrible, I say trash it.
    //
    
$template[$begin-1] = '';
    
$template[$end+1] = '';
    
    return 
$template;
}

/*******************************************************************************
 * function: show_template()                                                   *
 *   parameters: template [string]                                             *
 *   returns: nothing.                                                         *
 *   extra: be sure you've sent all headers before calling this.               *
 *     also, this will not assemble multiple templates you have parsed.        *
 *     do that individually.                                                   *
 *******************************************************************************/
function show_template($template)
{
    
// ok, all our work has done, let's just spit it out.
    
for($i=0$i count($template); $i++)
    {
        echo 
$template[$i];
    }
}