1
0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-06-27 13:23:25 +02:00

feat: introduce template engine (#2899)

This commit is contained in:
Dag
2022-07-08 14:17:25 +02:00
committed by GitHub
parent 951092eef3
commit abfc6b4633
9 changed files with 92 additions and 20 deletions

View File

@ -12,6 +12,44 @@
* @link https://github.com/rss-bridge/rss-bridge
*/
function render(string $template, array $context = []): string
{
$context['page'] = render_template($template, $context);
return render_template('base.html.php', $context);
}
function render_template(string $template, array $context = []): string
{
if (isset($context['template'])) {
throw new \Exception("Don't use `template` as a context key");
}
extract($context);
ob_start();
try {
require __DIR__ . '/../templates/' . $template;
} catch (\Throwable $e) {
ob_end_clean();
throw $e;
}
return ob_get_clean();
}
/**
* Escape for html context
*/
function e(string $s): string
{
return htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
/**
* Explicitly don't escape
*/
function raw(string $s): string
{
return $s;
}
/**
* Removes unwanted tags from a given HTML text.
*