PHP Classes
elePHPant
Icontem

File: simpletemplate.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of J.B.  >  Simple Object  >  simpletemplate.php  >  Download  
File: simpletemplate.php
Role: Auxiliary script
Content type: text/plain
Description: class SimpleTemplate provides a way to avoid typing PHP in line with HTML. This class extends SimpleObject class
Class: Simple Object
Base class with common variable access functions
Author: By
Last change: 1) Added support for template strings
2) Now you can instantiate SimpleTemplate object and specify it's variables as an associative array
3) optimized fetch() method to avoid making copies of all variables. now it uses array_keys function to retrieve variable keys.
Date: 10 years ago
Size: 870 bytes
 

Contents

Class file image Download
<?php
   
if ( !defined('SO_TEMPLATE_FILE') ) {
       
define('SO_TEMPLATE_FILE',1);
       
define('SO_TEMPLATE_STRING',2);
    }

    class
SimpleTemplate extends SimpleObject {
       
        function
SimpleTemplate($item_data=array()) {
           
parent::SimpleObject(SO_NOT_STRICT);
           
$this->import($item_data);
        }
       
        function
fetch($template,$template_mode=SO_TEMPLATE_FILE) {
            switch (
$template_mode ) {
                case
SO_TEMPLATE_STRING:
                   
$src = $template;
                    break;
                case
SO_TEMPLATE_FILE:
                default:
                   
$src = file_get_contents($template);
                    break;
            }
           
$keys = array_keys($this->vars);
            foreach (
$keys as $key ) {
               
$src = str_replace('<!--$'.$key.'-->',$this->get($key),$src);
            }
            return
$src;
        }
       
        function
display($template_file,$template_mode=SO_TEMPLATE_FILE) {
            echo
$this->fetch($template_file,$template_mode);
        }
    }
?>