<?php
// template.php
class template {
private $tpl_text;
private $tpl_values = array();
public function __construct($tpl_file) {
try {
if (!file_exists($tpl_file)) {
throw new Exception("template unknown<br>\n");
}
$tpl_text = file_get_contents($tpl_file);
$tpl_text = str_replace("<?php return; ?>", '', $tpl_text);
$this->tpl_text = $tpl_text;
return true;
} catch (Exception $e) {
echo $e->getMessage(), "\n";
return false;
}
}
public function fill($index, $value) {
$this->tpl_values[$index] = str_replace("##", "<br>", $value);
}
public function show() {
return $this->tpl_text;
}
public function process_placeholders() {
$tpl_text = $this->tpl_text;
$tpl_text = str_replace("##", "<br>", $tpl_text);
foreach ($this->tpl_values as $index => $value) {
$tpl_text = str_replace("[@$index]", $value, $tpl_text);
$tpl_text = str_replace("[/@$index]", "", $tpl_text);
}
$this->tpl_text = $tpl_text;
return;
}
public function process_loop($loopvar, $array, $mark = "") {
if (empty($array)) return;
$tpl_text = $this->tpl_text;
$tpl_text = str_replace("##", "<br>", $tpl_text);
$lenvar = strlen("[/@$loopvar]");
$start = strpos($tpl_text, "[@$loopvar]");
$end = strpos($tpl_text, "[/@$loopvar]");
$front = substr($tpl_text, 0, $start);
$back = substr($tpl_text, $end + $lenvar, strlen($tpl_text)- $end + $lenvar);
$tpl_textblock = substr($tpl_text, $start, ($end + $lenvar) - $start);
$tpl_textblock = str_replace("[@$loopvar]", "", $tpl_textblock);
$tpl_textblock = str_replace("[/@$loopvar]", "", $tpl_textblock);
$tpl_text = str_replace($tpl_textblock, "", $tpl_text);
$middle = "";
foreach($array as $index => $value) {
$temp = $tpl_textblock;
if (is_array($value)) {
foreach($value as $key => $content) {
$temp = str_replace("[@$key]", $content, $temp);
}
} elseif (is_object($value)) {
foreach($value as $property => $propertyvalue) {
$temp = str_replace("[@$property]", $propertyvalue, $temp);
}
} else {
$temp = str_replace("[@$mark]", $value, $temp);
}
$middle .= $temp;
}
$this->tpl_text = $front . $middle . $back;
return;
}
}
<?php return; ?>
<link rel='stylesheet' type='text/css' href='w3.css'>
<div>
<a href="index.php" class="w3-left w3-btn w3-border w3-pale-green w3-large w3-round-xlarge">Home</a>
[@items]
<a href="[@item_url]" class="w3-btn w3-left w3-border w3-large w3-round-xlarge [@item_color]">
[@item]
</a>
[/@items]
</div>
// show a navigation bar using the template class
require("database.php");
if (!isset($db)) $db = new database();
require "category.php";
$catobj = new category();
$categories = $catobj->all_active_records($db);
$list = [];
foreach($categories as $objcat) {
$url = "index.php?c=".$objcat->id;
$list[] = array("item"=>$objcat->name, "item_url"=>$url, "item_color"=>"w3-pale-yellow");
}
require "template.php";
$tpl_nav = new template("tpl_01.php");
$tpl_nav->process_loop("items",$list);
$tpl_nav->process_placeholders();
echo $tpl_nav->show();
<?php return; ?>
<link rel='stylesheet' type='text/css' href='w3.css'>
[@snippets]
[@paragraph]
[/@snippets]
Het tweede template bestand dient om elke paragraaf van de nodige stijlen te voorzien: tpl_03.php:
<?php return; ?>
<div class="w3-center w3-panel w3-padding w3-large w3-pale-red w3-card w3-round-large">
<div style="text-align:left;">[@paragraph]</div>
</div>
<?php
// show_snippets.php
require "template.php";
$snippets_array = array();
// the paragraphs can be read from a text file or from records in a database
$textpars = array("This is a first paragraph.##Second line.",
"This is a second paragraph.##New line.");
foreach($textpars as $textpar) {
$tpl_textparagraph = new template("tpl_03.php");
$tpl_textparagraph->fill("paragraph", $textpar);
$tpl_textparagraph->process_placeholders();
$textparagraph_result = $tpl_textparagraph->show();
$snippets_array[] = array("paragraph"=>$textparagraph_result);
}
$tpl_codesnippet = new template("tpl_02.php");
$tpl_codesnippet->process_loop("snippets", $snippets_array);
$tpl_codesnippet->process_placeholders();
echo $tpl_codesnippet->show();
<?php return; ?>
<link rel='stylesheet' type='text/css' href='w3.css'>
<div class="w3-container w3-padding">
<table class="w3-table w3-table-all" style="width:20%;">
<tr>
<th>Id</th><th>Name</th><th>Active</th>
</tr>
[@items]
<tr>
<td>[@id]</td><td>[@name]</td><td>[@active]</td>
</tr>
[/@items]
</table>
</div>
// list of active category records
require("database.php");
if (!isset($db)) $db = new database();
require "category.php";
$catobj = new category();
$categories = $catobj->all_active_records($db);
//var_dump($categories);
require "template.php";
$tpl_list = new template("tpl_04.php");
$tpl_list->process_loop("items",$categories);
$tpl_list->process_placeholders();
echo $tpl_list->show();