@ 2012-10-17T19:40:33Z <?php
class Template {
public $dir = '';
private $extension = 'php';
private $template = '';
private $parse;
function __construct($dir = false, $extension = 'php') {
if ($dir) {
$this->dir = preg_replace('#/+$#', '', $dir);
} else {
$this->dir = $dir;
}
$this->extension = $extension;
}
public function load() {
if (func_num_args() > 0) {
$files = func_get_args();
foreach ($files as $file) {
if (is_file($this->dir . '/' . $file . '.' . $this->extension)) {
$this->template .= file_get_contents($this->dir . '/' . $file . '.' . $this->extension);
} else {
trigger_error('Шаблона ' . htmlspecialchars($file . '.' . $this->extension) . ' не существует.', E_USER_WARNING);
}
}
}
}
public function clear() {
$this->template = '';
return true;
}
private function parseInclude($template) {
//Парсим блоки {include=file}
$count = preg_match_all('/{include=([\w\/]+)}/i', $template, $match);
if ($count > 0) {
$count -= 1;
for ($i = 0; $i <= $count; $i++) {
$file = $this->dir . '/' . $match[1][$i] . '.' . $this->extension;
if (is_file($file)) {
$template = str_replace($match[0][$i], $this->parseInclude(file_get_contents($file)), $template);
} else {
$template = str_replace($match[0][$i], '', $template);
trigger_error('Ошибка вставки шаблона <b>' . htmlspecialchars($match[1][$i]) . '</b>. Шаблона <b>' . htmlspecialchars($file) . '</b> не существует.', E_USER_WARNING);
}
}
}
return $template;
}
private function parseBlock($parse, $template) {
$c = preg_match_all('#\[([\w\-\!]+)\](.*?)\[/\1\]#is', $template, $match);
if ($c > 0) {
$c -= 1;
for ($i = 0; $i <= $c; $i++) {
if ($match[1][$i]{0} == '!') {
if (isset($parse[substr($match[1][$i], 1)]))
$match[2][$i] = '';
}
else {
if (!isset($parse[$match[1][$i]]))
$match[2][$i] = '';
}
$template = str_replace($match[0][$i], $match[2][$i], $template);
}
$template = $this->parseBlock($parse, $template);
}
return $template;
}
public function parse($parse) {
if (empty($this->template)) {
trigger_error('Шаблон не задан или он пустой.', E_USER_WARNING);
} else {
$this->parse = $parse;
$template = $this->parseInclude($this->template);
$template = $this->parseBlock($parse, $template);
return preg_replace('#\{([a-z0-9\-_]*?)\}#Ssie', '( ( isset($parse[\'\1\']) ) ? $parse[\'\1\'] : \'\' );', $template);
}
}
}
?>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Fatal error: Array and string offset access syntax with curly braces is no longer supported in /in/f6jrR on line 60
Process exited with code 255 . Output for 7.4.0 - 7.4.25 , 7.4.27 - 7.4.33 Deprecated: Array and string offset access syntax with curly braces is deprecated in /in/f6jrR on line 60
Output for 5.0.0 - 5.0.5 , 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.0 - 5.5.36 , 5.6.0 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.20 , 7.2.6 - 7.2.33 , 7.3.12 - 7.3.33 , 7.4.26 Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/f6jrR on line 5
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/f6jrR on line 5
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/f6jrR on line 5
Process exited with code 255 . preferences:dark mode live preview
242.64 ms | 401 KiB | 346 Q