Cursussen/Courses Codesnippets     Top 
PHP - Template class


1. Template class
You can use this class to display standard pieces of HTML code.
The HTML code is contained in separate 'template' files.
A template file is read into the constructor and the text is placed in the $tpl_text property.
The fill($index, $value) method fills the $tpl_values property (an array) with the necessary values.
The template file contains special markers (placeholders) that are replaced with the correct values using the process_placeholders() method.
With the method process_loop($loopvar, $array, $mark) part of the template is repeatedly filled in with the values of the array. The $loopvar variable determines which piece to handle.
The $loopvar variable is denoted in the template with the syntax [@name] and the end of the loop is denoted by [/@name].
<?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;
	}
}


2. Using a navigation bar template
This first example shows a navigation bar on the screen.
The data comes from the database table 'category' where all active records are retrieved. The composite list is then used to execute the loop of the template.
The code uses a template file: tpl_01.php:
<?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();


3. Text paragraphs
In this second example, text paragraphs are shown.
The code uses 2 template files.
The first template file contains a loop for the paragraphs: tpl_02.php:
<?php return; ?>
<link rel='stylesheet' type='text/css' href='w3.css'>
[@snippets]
	[@paragraph]
[/@snippets]
The second template file serves to provide each paragraph with the necessary styles: 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();


4. A list of records
The third example shows a list of records from a database table.
In this case the 'category' table and all active records.
The array of objects ($categories) is used in the loop processing. The template file contains the necessary placeholders for each field of the table: tpl_04.php
<?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();