Da der (unter Mozilla) so schlecht zu lesen ist:
Die Klasse ist wohl die simpelste Template Engine die man sich vorstellen kann.
Sie ersetzt lediglich Platzhalter mit dem vorher bestimmten Inhalt, ohne eval() variablen Variablen oder sonstigen Übeln.
Dies können normale Variablen, Arrays oder Methodenaufrufe sein,.
OffTopic:
--------------------------------------------------------------------------------
Wär schön wenn der blöde BBCODE meinen Code in ruhe lassen könnte
--------------------------------------------------------------------------------
PHP:--------------------------------------------------------------------------------
<?php
/***********************************************************************
* @author Clemens Krack <ckrack@i-z.de>
* @link <a href="http://www.tripdown.de" target="_blank">http://www.tripdown.de</a>
* @copyright Clemens Krack 2004
* @since 09.05.2004
* @version $Id smalltemplate.class.php, v 1.0, 09.05.2004 15:14:18 rythms Exp $
***********************************************************************
*
* use {$varname} to display previously assigned: "varname".
* use {$arrname.field} to display the index "field" of previously assigned array "arrname".
* use {$objname->method()} to display the output of the method "method" of previously assigned object : "objname".
*
* @example
*
* // test.tpl:
* ------------------------------------------------------------
{$foo} blablabla {$bar}
{$arr.field}
{$obj->foo()}
* ------------------------------------------------------------
*
* // test.php:
* ------------------------------------------------------------
class test{
function foo()
{
return "test::foo() returns this text.";
}
}
require_once('smalltemplate.class.php');
$foo = "foo's value is bar.";
$bar = "bar's value is foo.";
$tpl = new smalltemplate();
$tpl->assign('foo', $foo);
$tpl->assign('bar', $bar);
$fo = new test();
$tpl->assign('obj', $fo);
$arr = Array('field' => 'arr["field"] 's value is foobar.');
$tpl->assign('arr', $arr);
echo $tpl->display('test.tpl');
* ------------------------------------------------------------
*
* // output:
* ------------------------------------------------------------
foo's value is bar. blablabla bar's value is foo.
arr["field"] 's value is foobar.
test::foo() returns this text.
* ------------------------------------------------------------
*
***********************************************************************/
/**
* smalltemplate
*
* simple template class
* can replace variables, array indexes and method calls.
*
* @author clemens krack <ckrack@i-z.de>
**/
class smalltemplate {
var $template_dir;
var $tag_start = "{";
var $tag_end = "}";
/**
* smalltemplate::smalltemplate()
*
* @param string $tpl_dir
* path to the template dir, from where template files will be read.
* without trailing slash.
*
* @return void
**/
function smalltemplate($tpl_dir = NULL)
{
if (is_null($tpl_dir)) {
$this->template_dir = dirname(__FILE__);
} else {
$this->template_dir = $tpl_dir;
}
}
/**
* smalltemplate::assign()
*
* saves passed name and value in the variable stack.
* experimentally: passes value by reference.
* you must save the value(s) in a variable before passing it.
*
* @param string $name
* variable name inside the template
*
* @param variable $value
* variable containing value(s)
*
* @return void
**/
function assign($name, &$value)
{
// save the value of an assigned variable
$this->_vars[$name] = $value;
}
/**
* smalltemplate::_replace_var()
* private function to replace normal variables.
* @access private
**/
function _replace_var($a)
{
if (isset($this->_vars[$a[1]])) {
// return assigned template variable (named in first match)
return $this->_vars[$a[1]];
} else {
// template var not assigned
trigger_error("'{$a[1]}' not assigned as template variable. Use template::assign() before using variables in your templates", E_USER_WARNING);
return NULL;
}
}
/**
* smalltemplate::_replace_array()
* private function to replace array indexes.
* @access private
**/
function _replace_array($a)
{
if (isset($this->_vars[$a[1]][$a[2]])) {
// return assigned template variable's field (named in second match)
return $this->_vars[$a[1]][$a[2]];
} else {
// template var not assigned
trigger_error("'{$a[1]}.{$a[2]}' not assigned as template variable. Use template::assign() before using variables in your templates", E_USER_WARNING);
return NULL;
}
}
/**
* smalltemplate::_replace_methods()
* private function to replace method calls.
* @todo implement method_exists() check.
* @access private
**/
function _replace_methods($a)
{
echo "<pre>"; var_dump($a); echo "</pre>";
if (isset($this->_vars[$a[1]])) {
// return assigned template variable's field (named in second match)
return call_user_func(Array(&$this->_vars[$a[1]], $a[2]));
} else {
// template var not assigned
trigger_error("'{$a[1]}.{$a[2]}' not assigned as template variable. Use template::assign() before using variables in your templates", E_USER_WARNING);
return NULL;
}
}
/**
* smalltemplate::display()
*
* returns a string taken from parsing the template file passed as $tpl.
*
* @param string $tpl
* name of the template file to parse.
*
* @return string
**/
function display($tpl)
{
// template file exists and can be read?
if ((!file_exists($this->template_dir . "/" . $tpl)) OR !is_readable($this->template_dir . "/" . $tpl)) {
trigger_error("template file does not exist. skipping.", E_USER_WARNING);
return;
}
// open template file
$string = file_get_contents($this->template_dir . "/" . $tpl);
// quote special regexp chars in used defined start/end tags
$tag_start = preg_quote($this->tag_start);
$tag_end = preg_quote($this->tag_end);
// define the search pattern.
$pattern = '='.$tag_start.'$(w+)'.$tag_end.'=';
// replace {$var} 's
$string = preg_replace_callback($pattern, array(&$this, '_replace_var'), $string);
// define the search pattern.
$pattern = '='.$tag_start.'$(w+).(w+)'.$tag_end.'=';
// replace {$array.index} 's
$string = preg_replace_callback($pattern, array(&$this, '_replace_array'), $string);
// define the search pattern.
$pattern = '='.$tag_start.'$(w+)->(w+)()(;*)'.$tag_end.'=';
// replace {$obj->method();} 's
$string = preg_replace_callback($pattern, array(&$this, '_replace_methods'), $string);
return $string;
}
} // class smalltemplate
?>
Partnerseiten
