PHP Classes
elePHPant
Icontem

File: template.class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Ralf Mike Pretzlaw  >  Template Class / Viewer  >  template.class.php  >  Download  
File: template.class.php
Role: Class source
Content type: text/plain
Description: Template-Class
Class: Template Class / Viewer
Template engine that replaces marks with variables
Author: By
Last change: mail
Date: 6 years ago
Size: 4,438 bytes
 

Contents

Class file image Download
<?php
/**
 * Template-Class
 *
 * With this class you can seperate the PHP-source from the design of your
 * page. Just include this script and you are able to make objects, that
 * contain the design of your page with some placeholder like: {placeholder}
 *
 * To fill them just act as follows:
 * // Create the new Object
 * $tpl = new Template("the_template_file.html");
 * // Give the placeholder it's value / content
 * $tpl->placeholder = "Hello World!";
 *
 * Remark that you set the value without the initiating and concluding chars,
 * that are set in the globals "TEMPLATE_OPEN" and "TEMPLATE_CLOSE".
 *
 * You can also fill the placeholder with another template:
 * // Bind a template to the placeholder
 * $tpl->placeholder = new Template("another_template.html");
 * // Access to the new placeholders
 * $tpl->placeholder->placeholer_of_the_other_template = "foo";
 *
 * You can repeat this as often / "deep" as you want.
 *
 * Since version 1.2.0 you can add PHP-Scripts as an placeholder for your
 * template. It's output (after execution) will be set for the placeholders
 * value:
 * // Add placeholder "foo" to template
 * $tpl->foo = new Template("script.php");
 *
 * This has been added just to enhance the possibilities of this class. To use
 * PHP as a placeholder is not the sense of templates!
 *
 * ATTENTION : Do NOT use ob_* within the PHP script, which shall be included.
 * All PHP scripts are standalone-scripts. So if you want to use a variable from
 * the mainscript you'll have to make it global in the template-script:
 * global $var_name;
 *
 *
 * LICENSE: This source file is subject to
 * "Attribution-Noncommercial-No Derivative Works 3.0 Unported" license
 * that is available through the world-wide-web at the following URI:
 * http://creativecommons.org/licenses/by-nc-nd/3.0/
 * If you did not receive a copy of the license and are unable to
 * obtain it through the web, please send a note to mikescript@gmx.de
 * so I can mail you a copy.
 * Put this always visible and reachable on your page:
 * Template 1.2.1 (c) Pretzlaw
 *
 * 2009/02/13 - Ralf Mike Pretzlaw
 * @category Template
 * @author Ralf Mike Pretzlaw
 * @copyright Ralf Mike Pretzlaw
 * @license Attribution-Noncommercial-Share Alike 3.0 Unported
 * @link http://creativecommons.org/licenses/by-nc-sa/3.0/
 * @version 1.2.0
 *
*/

/**
 * beginning of each template-variable
 * @var string
 */
define("TEMPLATE_OPEN", "{");
/**
 * end of each template-variable
 * @var string
 */
define("TEMPLATE_CLOSE", "}");
/**
 * directory to the templates
 * @var string
 */
define("TEMPLATE_DIR", "templates");
/**
 * directory to PHP files for templates
 * @var string
 */
define("TEMPLATE_PHP", "php");

class
Template
{
   
/**
     * Content of the template
     * @var string
     */
   
private $ustContent;
   
/**
     * List of placeholders and their content
     * @var array
     */
   
private $mpstVars = array();
   
   
/**
     * Constructor - opens template and stores it in object
     * @param $File string Filename or content
     * @param $IsContent bool Is content given? (default: false)
     */
   
function __construct($File, $IsContent = false)
    {
        if (!
$IsContent)
        {
           
$ext = pathinfo($File, PATHINFO_EXTENSION);
            if (
stripos($ext, "php") === false)
            {
               
$this->ustContent = file_get_contents(TEMPLATE_DIR . "/" . $File);
            }
            else
            {
               
ob_start();
                include(
TEMPLATE_PHP . "/" . $File);
               
$this->ustContent = ob_get_clean();
            }
        }
        else
        {
           
$this->ustContent = $File;
        }
    }
   
   
/**
     * sets a new placeholder and it's value
     *
     * @param $var string
     * @param $val string
     */
   
function __set($var, $val)
    {
       
$this->mpstVars[TEMPLATE_OPEN . $var . TEMPLATE_CLOSE] = $val;
    }
   
   
/**
     * gets a placeholders value
     * @param $var string
     * @return variant
     */
   
function __get($var)
    {
        return
$this->mpstVars[TEMPLATE_OPEN . $var . TEMPLATE_CLOSE];
    }
   
   
/**
     * Returns the template with the placeholders replaced by their value
     * @return string
     */
   
function __toString()
    {
        return
strtr($this->ustContent, $this->mpstVars);
    }
   
}

ob_start();

echo
"hallo";



$loc = "omotivw!";
$temp = new Template("foo.tpl.html");
$temp->foo = new Template("bar.php");
$temp->foo->here = "there";

echo
$temp;

?>