PhpSnip.com

User Stats

simple template function

This is simple template function which reduces developement time..

Info

 Download  View Source (print view)
 Rating : 4.7  Views : 212

Source Code ( 26 lines )

//this is the function that return the template content as a string  after replacing place holders[{}] to a array variable which has place holders as index.
//this fuction assumes that all template files in a folder templates and there is a varible definded $site_path to serverpath.
//$name is html file name that may be in templates folder
function do_template($name, $array="_var") 
{
    global $$array, $_config;

    //read the template files
    $content = read_file($site_path . '/templates/' . $name);

    foreach ($$array as $index => $value) {
        $content = str_replace("{$index}", $value, $content);
    }

    $content = preg_replace('#{([a-z0-9-_]*?)}#', '', $content);
    return $content;
}

//to read the content of a file
function read_file($file)
{
    $fp = fopen($file, 'r') or die("File Does'nt Exists");
    $content = fread($fp, filesize($file));
    fclose($fp);
    return $content;
}

Search

Subscribe

  Rss Feeds

Sponsors

Advertise