a simple template class to separate design from code (mvc). similar to the ever popular smarty framework, just not nearly as big lol
example.html
<html>
<body>This [@WOULD] be [@EXAMPLE]</body>
</html>
example.php
<?php
include('template.class.php');
$template = new Template('example.html');
$args = array( 'WOULD' => 'would', 'EXAMPLE' => 'an <i>example</i>' );
$template->set( $args );
$template->renderHTML();
?>
anything passed to set() needs to be in array format. when instantiating Template, argument needs to be location of html. the above example, the result of example.php would be:
This would be an example
<?php
/**
* @author Sean Wragg <seanwragg@gmail.com>
* @version 1.2 bRC3 template.class.php
*/
class Template {
protected $file;
public $values;
// attempt to locate file upon instantiation
function __construct( $file ) {
if ( !file_exists( $file ) ) die('unable to locate file');
$this->file = $file;
}
// arg must be an array
public function set( $values ) {
foreach( $values as $key => $value ) {
$this->values[$key] = $value;
}
}
// render's html with keys replaced
public function renderHTML() {
$contents = file_get_contents( $this->file );
foreach( $this->values as $key => $value ) {
$contents = str_replace( '[@'. $key .']', $value, $contents );
}
echo $contents;
}
}
?>