I just finished making a really light and simple template class that supports blocks and regular placeholder substitutions. I decided not to use the existing ones out there because I found them to be extremely bloated, not to mention I don't like using stuff that I don't understand completely. It would've taken me days to go through smarty or any other template engines out there and find out how they work (thousands of lines across several files :bemused: )
Anyway, I'm a beginner coder and would like some criticism. You can find all of the below files are also located here on my server.
Ok, this is the class. Click here to see the source on my server.
<?
class Template
{
var $template;
var $html;
var $replacements = array();
var $blocks = array();
var $newblocks = array();
function Template($template)
{
$this->template = $template;
$this->html = file_get_contents($this->template); // Reads the template into a string.
preg_match_all('#<!-- BEGIN (.*) -->(.*)<!-- END \1 -->#s', $this->html, $matches); // Parses through template and finds Blocks, if any.
for ($i=0;$i<count($matches[1]);$i++) {
$this->blocks[ $matches[1][$i] ] = $matches[2][$i]; // Puts blocks into its own array in the form of $this->blocks['block'] = 'Contents'
$this->newblocks[ $matches[1][$i] ] = '';
}
//print_r($this->blocks);
$this->html = preg_replace('#<!-- BEGIN (.*?) -->(.*?)<!-- END \1 -->#s', '{\1}', $this->html); // Strips Blocks from the string and replaces it with a regular template Variable in the form of {block}.
}
function setReplace($variable, $replace = null)
{
if (is_array($variable)) {
foreach ($variable as $key => $var) {
$this->replacements[$key] = $var;
}
}
else {
$this->replacements[$variable] = $replace;
}
}
function setBlock($replaceVars, $block) // Takes an array and uses the keys as the template variables and the values for the replacement.
{
if ( isset($this->blocks[ $block ]) ) {
$this->newblocks[$block] .= str_replace( explode( ',', ('{' . implode('},{', array_keys($replaceVars)) . '}') ) , $replaceVars, $this->blocks[ $block ] );
}
}
function publish()
{
foreach (array_keys($this->blocks) as $block) {
$this->html = str_replace ('{' . $block . '}', $this->newblocks[$block], $this->html);
}
foreach ($this->replacements as $key => $value) {
$this->html = str_replace ('{' . $key . '}', $value, $this->html);
}
echo $this->html;
}
}
?>
This is an example template. Click here to see the source on my server.
<table>
<tr>
<th>{NAMEHEAD}</th><th>{AGEHEAD}</th><th>{SEXHEAD}</th>
</tr>
<!-- BEGIN row -->
<tr>
<td>{name}</td>
<td>{age}</td>
<td>{sex}</td>
</tr>
<!-- END row -->
</table>
This is an example of the class being used along with the template above. Click here to see the source on my server.
<?
error_reporting (E_ALL);
// Usage:
//
// Template Placeholders should be in the form of {[0-9A-Za-z_-]+}
// Template Blocks should be in the form of (SEE BELOW)
// <!-- BEGIN [0-9A-Za-z_-]+ -->
// ... block content ...
// <!-- END [0-9A-Za-z_-]+ -->
$text = array(
'AGEHEAD' => 'Age',
'SEXHEAD' => "Sex",
);
$people[] = array(
'name' => "Dave",
'age' => "22",
'sex' => 'Male');
$people[] = array(
'name' => "Jennifer",
'age' => "21",
'sex' => 'Female');
$people[] = array(
'name' => "Gina",
'age' => "26",
'sex' => 'Female');
require_once ("Template.php");
$page = new Template ('example.tpl');
//works with both singe value pair replacement or Array (key, value) replacement.
$page->setReplace('NAMEHEAD', 'Name'); // Single Replacement.
$page->setReplace($text); // Multiple Replacements, sending an Array using Keys as Template Vars and Values as replacements.
// Replacing Block with an Array once again using Keys as Template Vars and Values as replacements.
foreach ($people as $person) {
$page->setBlock($person, 'row');
}
$page->publish();
?>
AND NOW FOR THE END RESULT
CLICK HERE