Просмотр поста

.
Koenig
(\/)____o_O____(\/)

Навелосипедил простой нативный шаблонизатор
Сам класс

class Tpl {
    
    private $dir;
    
    private $ext;
    
    public function __construct($dir, $ext) {
        $this->dir = $dir;
        $this->ext = $ext;
    }
   
    public function __call($name, $arguments) {
        $action = substr($name, 0, 3);
        $property = strtolower(substr($name, 3));
        switch ($action) {
            case 'get':
                return $this->$property;
                break;
                
            case 'set':
                $this->$property = $arguments[0];
                break;
                
            default :
                return false;
        }
    }
    
    public function args($args) {
        return call_user_func_array(array($this, 'render'), $args);
    }

    public function render($tpl, $data = null) {
        if (!is_null($data)) {
            foreach($data as $key => $val) {
                $k = 'set' . $key;
                $this->$k($val);
            }
        }
        
        ob_start();
        if (is_file($this->dir . DIRECTORY_SEPARATOR . $tpl . '.' . $this->ext)) {
            include($this->dir . DIRECTORY_SEPARATOR . $tpl . '.' . $this->ext);
        } else {
            echo '<h1>Tpl debug<h1><pre>' . print_r($data, 1) . '</pre>';
        }
        
        return ob_get_clean();        
    }
}

Шаблон
require_once('classes/tpl.php');

$tpl = new Tpl('tpl', 'tpl.php');
// задаем папку где будут лежать наши шаблоны и их расширение

$names = array('names' => array('Вася', 'Петя', 'Коля'));
$dirs = array('dirs' => array('images', 'cache', 'work'));
$test = array('test' => 'testus');

// вариант 1
$array = array_merge($names, $dirs, $test);

echo $tpl->render('test', $array);

// вариант 2
$tpl->setNames(array('Вася', 'Петя', 'Коля'));
$tpl->setDirs(array('images', 'cache', 'work'));
$tpl->setTest('testus');

echo $tpl->render('test');

скрин
Прикрепленные файлы: