1
0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-08-05 08:07:33 +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

@@ -58,7 +58,9 @@ class Authentication
if (Configuration::getConfig('authentication', 'enable') === true) {
if (!Authentication::verifyPrompt()) {
header('WWW-Authenticate: Basic realm="RSS-Bridge"', true, 401);
die('Please authenticate in order to access this instance !');
$message = 'Please authenticate in order to access this instance !';
print $message;
exit;
}
}
}

View File

@@ -317,7 +317,10 @@ final class Configuration
*/
private static function reportError($message)
{
header('Content-Type: text/plain', true, 500);
die('Configuration error' . PHP_EOL . $message);
http_response_code(500);
print render('error.html.php', [
'message' => "Configuration error: $message",
]);
exit;
}
}

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.
*