2013-02-24, 19:23:52
Since I had to put a delay in my GS development... here is the code for the Template for the curious ones (it's slightly more than 1 KB :-) :
A template looks like this:
And can be called in the following way:
You can put any php code into the template but you should really really try to avoid anything fancier than ifs, foreachs and echoes.
ciao
a.l.e
p.s.: it's trivial code, so consider it public domain...
p.p.s.: i have versions of it that optionally read from a string instead of a file...
p.p.p.s.: i have not tested the sample code above
PHP Code:
<?php
class Template {
var $vars;
function Template() {
}
public static function factory() {
return new Template();
}
/**
* Clear the templates variables
*/
function clear() {
unset($this->vars);
return $this;
}
/**
* Set a template variable.
*/
function set($name, $value) {
$this->vars[$name] = $value;
return $this;
}
/**
* Open, parse, and return the template file.
*
* @param $_p_file string the template file name
* (name obfuscated to avoid clashes with template variables names)
*/
function fetch($_p_file) {
if (!empty($this->vars)) {
extract($this->vars); // Extract the vars to local namespace
}
ob_start(); // Start output buffering
if (is_readable($_p_file)) {
include($_p_file); // Include the file
} else {
debug('could not find file', $_p_file);
}
$contents = ob_get_contents(); // Get the contents of the buffer
ob_end_clean(); // End buffering and discard
return $contents; // Return the contents
}
} // Template()
A template looks like this:
PHP Code:
<html>
<head>
<tittle><?= $title ?></title>
</head>
<body>
<h1><?= $title ?></h1>
<?= $content ?>
</body>
</html>
And can be called in the following way:
PHP Code:
$content = '<p>...</p>';
Template::factory()->
set('title', 'my fancy title')->
set('content', $content)->
fetch('filename.php');
You can put any php code into the template but you should really really try to avoid anything fancier than ifs, foreachs and echoes.
ciao
a.l.e
p.s.: it's trivial code, so consider it public domain...
p.p.s.: i have versions of it that optionally read from a string instead of a file...
p.p.p.s.: i have not tested the sample code above