Kod
class Template {
var $data;
var $template;
function Template () {
$this->data = Array();
}
function addVar ($name, $value) {
if (is_array($value)) {
$this->data[$name] = Array();
array_push($this->data[$name], $value);
}
else if (!empty($value)) {
$this->data[$name] = $value;
}
}
private function getIncluded ($match) {
return implode('', file($match[1]));
}
private function getVars ($match) {
return $this->data[$match[1]];
}
private function includeTemplates () {
$this->template = preg_replace_callback('/{include->([^>][0-9a-zA-z._]+)}/', Array($this,'getIncluded'), $this->template);
}
private function includeVars () {
$this->template = preg_replace_callback('/{([^}]+)}/', Array($this, 'getVars'), $this->template);
}
private function fillTable ($match) {
$table = '<table>'."\n";
if ((isset($match[2])) && ($match[2] != null) ) {
$headers = $this->data[$match[2]][0];
if (is_array($headers)) {
$table .= '<tr>';
foreach ($headers as $header) {
$table .= '<th>'.$header.'</th>';
}
$table .= '</tr>'."\n";
} else {
$table .= '<tr><th>'.$headers.'</th></tr>'."\n";
}
}
$data = $this->data[$match[1]][0];
if (is_array($data)) {
foreach ($data as $row) {
if (is_array($row)) {
$table .= '<tr>';
foreach ($row as $col) {
$table .= '<td>'.$col.'</td>';
}
$table .= '</tr>'."\n";
} else {
$table .= '<tr><td>'.$row.'</td></tr>'."\n";
}
}
}
$table .= '</table>'."\n";
return $table;
}
private function printTables () {
$this->template = preg_replace_callback('/{table->([^>][0-9a-zA-Z_]+),?([^,][0-9a-zA-Z_]+)?}/', Array($this, 'fillTable'), $this->template);
}
function display ($template) {
$this->template = implode('', file($template));
while (preg_match('/{include->([^>][0-9a-zA-z._]+)}/', $this->template)) {
$this->includeTemplates();
}
$this->printTables();
$this->includeVars();
echo $this->template;
}
}
var $data;
var $template;
function Template () {
$this->data = Array();
}
function addVar ($name, $value) {
if (is_array($value)) {
$this->data[$name] = Array();
array_push($this->data[$name], $value);
}
else if (!empty($value)) {
$this->data[$name] = $value;
}
}
private function getIncluded ($match) {
return implode('', file($match[1]));
}
private function getVars ($match) {
return $this->data[$match[1]];
}
private function includeTemplates () {
$this->template = preg_replace_callback('/{include->([^>][0-9a-zA-z._]+)}/', Array($this,'getIncluded'), $this->template);
}
private function includeVars () {
$this->template = preg_replace_callback('/{([^}]+)}/', Array($this, 'getVars'), $this->template);
}
private function fillTable ($match) {
$table = '<table>'."\n";
if ((isset($match[2])) && ($match[2] != null) ) {
$headers = $this->data[$match[2]][0];
if (is_array($headers)) {
$table .= '<tr>';
foreach ($headers as $header) {
$table .= '<th>'.$header.'</th>';
}
$table .= '</tr>'."\n";
} else {
$table .= '<tr><th>'.$headers.'</th></tr>'."\n";
}
}
$data = $this->data[$match[1]][0];
if (is_array($data)) {
foreach ($data as $row) {
if (is_array($row)) {
$table .= '<tr>';
foreach ($row as $col) {
$table .= '<td>'.$col.'</td>';
}
$table .= '</tr>'."\n";
} else {
$table .= '<tr><td>'.$row.'</td></tr>'."\n";
}
}
}
$table .= '</table>'."\n";
return $table;
}
private function printTables () {
$this->template = preg_replace_callback('/{table->([^>][0-9a-zA-Z_]+),?([^,][0-9a-zA-Z_]+)?}/', Array($this, 'fillTable'), $this->template);
}
function display ($template) {
$this->template = implode('', file($template));
while (preg_match('/{include->([^>][0-9a-zA-z._]+)}/', $this->template)) {
$this->includeTemplates();
}
$this->printTables();
$this->includeVars();
echo $this->template;
}
}