Простой нативный шаблонизатор

Описание...


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');

Шаблон

<?php
echo '<h1>test template</h1>';
echo $this->getTest();
echo '<p>Names</p>';
foreach($this->getNames() as $name) {
echo '<div>' . $name . '</div>';
}

echo '<p>Dirs</p>';
foreach($this->getDirs() as $dir) {
echo '<div>' . $dir . '</div>';
}

echo '<h1>Debug</h1>';

echo '<pre>' . print_r($this, 1) . '</pre>';


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');

Шаблон

<?php
echo '<h1>test template</h1>';
echo $this->getTest();
echo '<p>Names</p>';
foreach($this->getNames() as $name) {
echo '<div>' . $name . '</div>';
}

echo '<p>Dirs</p>';
foreach($this->getDirs() as $dir) {
echo '<div>' . $dir . '</div>';
}

echo '<h1>Debug</h1>';

echo '<pre>' . print_r($this, 1) . '</pre>';


  14.01.24 / 13:29 | PHP |   110 | 2   0