- file_get_contents: documentation ( source)
- str_replace: documentation ( source)
- preg_replace: documentation ( source)
<?php
class MicroTemplateEngine {
public $data = array();
private $tpl;
function __construct($f) {
$this->tpl = $f;
}
function __set($name, $value) {
$this->data[$name] = $value;
}
function __toString() {
$out = file_get_contents($this->tpl);
foreach ($this->data as $_key => $_value) {
$out = str_replace('#{' . $_key . '}', $_value, $out);
}
$out = preg_replace('/#{.*?}/' , '' , $out );
return $out;
}
}
$t = new MicroTemplateEngine('<div>{inner}{{inner}}</div>');
$t->inner = array('inner' => 'oi');
echo $t;