Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 9th, 2012  |  syntax: None  |  size: 2.16 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3.   +----------------------------------------------------------------------+
  4.   | PHP.net Website Systems                                              |
  5.   +----------------------------------------------------------------------+
  6.   | Copyright (c) 2011 The PHP Group                                     |
  7.   +----------------------------------------------------------------------+
  8.   | This source file is subject to version 3.01 of the PHP license,      |
  9.   | that is bundled with this package in the file LICENSE, and is        |
  10.   | available through the world-wide-web at the following url:           |
  11.   | http://www.php.net/license/3_01.txt                                  |
  12.   | If you did not receive a copy of the PHP license and are unable to   |
  13.   | obtain it through the world-wide-web, please send a note to          |
  14.   | license@php.net so we can mail you a copy immediately.               |
  15.   +----------------------------------------------------------------------+
  16.   | Author:                                                              |
  17.   |     Kalle Sommer Nielsen <kalle@php.net>                             |
  18.   | Based on code by:                                                    |
  19.   |     Jim Winstead <jimw@php.net>                                      |
  20.   +----------------------------------------------------------------------+
  21. */
  22.  
  23. class Template implements ArrayAccess {
  24.         protected $name;
  25.         protected $vars = Array();
  26.  
  27.         public function __construct($template) {
  28.                 $this->name = $template;
  29.         }
  30.  
  31.         public function __toString() {
  32.                 return (string) $this->parse();
  33.         }
  34.  
  35.         public function parse() {
  36.                 ob_start();
  37.                 extract($this->vars, EXTR_SKIP);
  38.                 include 'templates/' . $this->name . '.tpl';
  39.  
  40.                 return ob_get_clean();
  41.         }
  42.  
  43.         public function offsetGet($offset) {
  44.                 if (isset($this->vars[$offset])) {
  45.                         return $this->vars[$offset];
  46.                 }
  47.         }
  48.  
  49.         public function offsetSet($offset, $value) {
  50.                 $this->vars[$offset] = (string) $value;
  51.         }
  52.  
  53.         public function offsetUnset($offset) {
  54.                 if (isset($this->vars[$offset])) {
  55.                         unset($this->vars[$offset]);
  56.                 }
  57.         }
  58.  
  59.         public function offsetExists($offset) {
  60.                 return isset($this->vars[$offset]);
  61.         }
  62. }
  63.  
  64. ?>