class.template.php
------------------------------------
[php:1:3ba2db3e88]<?php
class Template {
var $tmpl;
var $dane;
function Template ($name) {
$this->tmpl = implode('', file($name));
$this->dane = Array();
}
function add($name, $value = '') {
if (is_array($name)) {
$this->dane = array_merge($this->dane, $name);
} else if (!empty($value)) {
$this->dane[$name] = $value;
}
}
function execute() {
return preg_replace('/{([^}]+)}/e', '$this->dane["1"]', $this->tmpl);
}
}
?>[/php:1:3ba2db3e88]
test.php
------------------------------------
[php:1:3ba2db3e88]<?php
include 'class.template.php';
$tmpl = new Template('test.tpl');
$tmpl->add('title', 'tytuł strony');
$tmpl->add('autor', 'imie autora');
$tmpl->add('charset', 'iso-8859-2');
$dane = Array('imie'=> 'Marcin', 'podpis'=>'jakiś tam podpis');
$tmpl->add($dane);
echo $tmpl->execute();
?>[/php:1:3ba2db3e88]
test.tpl
------------------------------------
Kod
<html>
<head>
<title>{title}</title>
<meta http-equiv="Content-type" content="text/html; charset={charset}">
</head>
<body>
<p>Autorem strony jest {autor}</p>
<p>Witaj {imie}</p>
<p>{podpis}</p>
</body>
</html>
No to jest taki banalny :-) A jeżeli chodzi o najlepszy system szablonów, to
SMARTY. :-)