41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?php
|
|
namespace YTKNS;
|
|
|
|
final class Template {
|
|
private static array $vars = [];
|
|
|
|
public static function setVar(string $name, $value) {
|
|
return $vars[$name] = $value;
|
|
}
|
|
|
|
public static function prefixArray(array $input): array {
|
|
$output = [];
|
|
|
|
foreach($input as $key => $val)
|
|
$output[':' . $key] = $val;
|
|
|
|
return $output;
|
|
}
|
|
|
|
public static function renderRaw(string $path, array $vars = []): string {
|
|
$vars = self::prefixArray(array_merge(self::$vars, $vars));
|
|
$tpl = file_get_contents(YTKNS_TPL . '/' . $path . '.html');
|
|
$tpl = strtr($tpl, $vars);
|
|
return $tpl;
|
|
}
|
|
|
|
public static function render(string $path, array $vars = []): void {
|
|
echo self::renderRaw($path, $vars);
|
|
}
|
|
|
|
public static function renderSet(string $path, array $varSets = []): string {
|
|
$html = '';
|
|
$tpl = file_get_contents(YTKNS_TPL . '/' . $path . '.html');
|
|
|
|
foreach($varSets as $vars)
|
|
$html .= strtr($tpl, self::prefixArray($vars));
|
|
|
|
return $html;
|
|
}
|
|
}
|