Files
BiveEngine/engine/Main/trait.templates.php
2025-12-24 19:19:01 +03:00

119 lines
4.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// Работа с шаблонами
defined('ROOT_DIR') || exit;
trait Templates {
// Загрузка шаблона
public function template_load($name, $variables = array(), $new = true)
{
$template_path = $this->template_touch_name($name);
return $this->template_load_by_path($template_path, $variables, $new);
}
private function template_touch_name($name): string
{
$template_path = ROOT_DIR . SLASH . PLAYAREA_DIR_NAME . SLASH . "templates" . SLASH . $name;
if(file_exists($template_path)) return $template_path;
$plugin_template_path = ROOT_DIR . SLASH . PLAYAREA_DIR_NAME . SLASH . "plugins" . SLASH . $name;
if(file_exists($plugin_template_path)) return $plugin_template_path;
return $template_path;
}
// Загрузка шаблона по его адресу
public function template_load_by_path($path, $variables = array(), $new = true)
{
// Загрузка шаблона из буффера
$cache = $this->template_cache_load($path, $variables, $new);
if($cache) return $cache;
// Начало буффера
ob_start();
global $b;
require $path;
$buffer = ob_get_contents();
ob_end_clean();
// Конец буффера
// Сохранение буффера в кеш
$rendered = $this->template_variables_apply($buffer, $variables);
if($new === false) $this->template_cache_save($path, $variables, $rendered);
// Вывод буффера пользователю
echo $rendered;
return $buffer;
}
// Применить значения переменных
private function template_variables_apply($content, $variables)
{
return preg_replace_callback('/{{\s*(\w+)\s*}}/', function($match) use ($variables) {
$key = $match[1];
return $variables[$key] ?? $match[0];
}, $content);
}
// Сохранить шаблон
private function template_cache_save($name, $variables, $content): bool
{
if(!TEMPLATE_CACHE) return false;
$path = $this->template_cache_get_full_path($name, $variables);
file_put_contents($path, $content);
return true;
}
// Есть ли шаблон с таким именем
private function template_cache_has($name, $variables): bool
{
$path = $this->template_cache_get_full_path($name, $variables);
return file_exists($path);
}
// Загрузка кешированного шаблона
private function template_cache_load($name, $variables, $new = false)
{
if(!TEMPLATE_CACHE) return false;
if(!$new && $this->template_cache_has($name, $variables)) {
$cache = $this->template_cache_get($name, $variables);
echo $cache;
return $cache;
}
return false;
}
// Получить кеш шаблона
private function template_cache_get($name, $variables): string
{
$path = $this->template_cache_get_full_path($name, $variables);
return file_get_contents($path);
}
// Получить полный путь до кеша шаблона
private function template_cache_get_full_path($name, $variables): string
{
$path = $this->get_storage_dir() . SLASH . "temp" . SLASH . "templates" . SLASH;
return $path . md5($name . json_encode($variables)) . ".php";
}
// Количество кешированных шаблонов
public function template_cache_count(): int
{
$path = $this->get_storage_dir() . SLASH . "temp" . SLASH . "templates" . SLASH;
$files = glob($path."/*");
return count($files);
}
// Удалить весь кеш шаблонов
public function template_cache_remove(){
$path = $this->get_storage_dir() . SLASH . "temp" . SLASH . "templates" . SLASH;
$files = glob($path."/*");
if (count($files) > 0) {
foreach ($files as $file) {
if (file_exists($file)) {
unlink($file);
}
}
}
}
}