Added possibility to add stylesheets and javascripts

This commit is contained in:
Jeroen De Meerleer 2023-03-23 11:32:46 +01:00
parent 9e03804a64
commit 530c29b400
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
4 changed files with 67 additions and 15 deletions

View File

@ -4,6 +4,7 @@
$black: #141414;
$orange: #d6972a;
$white: #f0f0f0;
$font: 'Fira Code', monospace;
html {
@ -13,6 +14,7 @@ html {
body {
margin-bottom: 60px; /* Margin bottom by footer height */
font-family: 'Mukta', sans-serif;
background-color: $white;
}
.footer {
position: absolute;

View File

@ -14,25 +14,13 @@ class DefaultController extends AbstractController
{
$return = $page->getPage($kernel, $slug);
$response = new Response('', (int)$return['status']);
return $this->render('/page.html.twig', [
'nav' => $return['nav'],
'header' => $return['header'],
'content' => $return['content'],
'title' => $return['title'],
'footer' => $return['footer'],
], $response);
return $this->render('/page.html.twig', $return, $response);
}
public function ErrorAction(Request $request, Page $page, KernelInterface $kernel, string $status)
{
$return = $page->getPage($kernel, 'error/' . $status);
$response = new Response('', (int)$status);
return $this->render('/page.html.twig', [
'nav' => $return['nav'],
'header' => $return['header'],
'content' => $return['content'],
'title' => $return['title'],
'footer' => $return['footer'],
], $response);
return $this->render('/page.html.twig', $return, $response);
}
}

View File

@ -11,6 +11,8 @@ class Page
public function getPage(KernelInterface $kernel, string $page)
{
$parsedown = new \Parsedown();
$return['js'] = [];
$return['css'] = [];
if(file_exists($kernel->getCacheDir() . '/pages/_header.md')) {
$return['header'] = $parsedown->text(file_get_contents($kernel->getCacheDir() . '/pages/_header.md'));
} elseif(file_exists($kernel->getCacheDir() . '/pages/_header.html')) {
@ -19,6 +21,19 @@ class Page
throw new NotFoundHttpException('Some required files were not found');
}
if(file_exists($kernel->getProjectDir() . '/public/assets/js/_header.js')) {
$return['js'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/js/_header.js'), true)),
'file' => '/assets/js/_header.js'
];
}
if(file_exists($kernel->getProjectDir() . '/public/assets/css/_header.css')) {
$return['css'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/css/_header.css'), true)),
'file' => '/assets/css/_header.css'
];
}
if(file_exists($kernel->getCacheDir() . '/pages/_nav.md')) {
$return['nav'] = $parsedown->text(file_get_contents($kernel->getCacheDir() . '/pages/_nav.md'));
} elseif(file_exists($kernel->getCacheDir() . '/pages/_nav.html')) {
@ -27,6 +42,19 @@ class Page
throw new NotFoundHttpException('Some required files were not found');
}
if(file_exists($kernel->getProjectDir() . '/public/assets/js/_nav.js')) {
$return['js'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/js/_nav.js'), true)),
'file' => '/assets/js/_nav.js'
];
}
if(file_exists($kernel->getProjectDir() . '/public/assets/css/_nav.css')) {
$return['css'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/css/_nav.css'), true)),
'file' => '/assets/css/_nav.css'
];
}
if(file_exists($kernel->getCacheDir() . '/pages/_footer.md')) {
$return['footer'] = $parsedown->text(file_get_contents($kernel->getCacheDir() . '/pages/_footer.md'));
} elseif(file_exists($kernel->getCacheDir() . '/pages/_footer.html')) {
@ -35,6 +63,19 @@ class Page
throw new NotFoundHttpException('Some required files were not found');
}
if(file_exists($kernel->getProjectDir() . '/public/assets/js/_footer.js')) {
$return['js'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/js/_footer.js'), true)),
'file' => '/assets/js/_footer.js'
];
}
if(file_exists($kernel->getProjectDir() . '/public/assets/css/_footer.css')) {
$return['css'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/css/_footer.css'), true)),
'file' => '/assets/css/_footer.css'
];
}
if(file_exists($kernel->getCacheDir() . '/pages/' . $page . '.md')) {
$return['content'] = $parsedown->text(file_get_contents($kernel->getCacheDir() . '/pages/' . $page . '.md'));
} elseif(file_exists($kernel->getCacheDir() . '/pages/' . $page . '.html')) {
@ -43,7 +84,18 @@ class Page
throw new NotFoundHttpException('Some required files were not found');
}
if(file_exists($kernel->getProjectDir() . '/public/assets/js/' . $page . '.js')) {
$return['js'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/js/' . $page . '.js'), true)),
'file' => '/assets/js/' . $page . '.js'
];
}
if(file_exists($kernel->getProjectDir() . '/public/assets/css/' . $page . '.css')) {
$return['css'][] = [
'sha384' => base64_encode(hash('sha384', file_get_contents($kernel->getProjectDir() . '/public/assets/css/' . $page . '.css'), true)),
'file' => '/assets/css/' . $page . '.css'
];
}
$titles = json_decode(file_get_contents($kernel->getCacheDir() . '/pages/titles.json'), true);
$return['title'] = $titles[$page] ?? '';

View File

@ -20,10 +20,20 @@
{% block styles %}
{{ encore_entry_link_tags('page') }}
{% if css is defined and css is not empty %}
{% for sheet in css %}
<link rel="stylesheet" href="{{ sheet.file }}" integrity="sha384-{{ sheet.sha384 }}">
{% endfor %}
{% endif %}
{% endblock %}
{% block scripts %}
{{ encore_entry_script_tags('page') }}
{% if js is defined and js is not empty %}
{% for script in js %}
<script src="{{ script.file }}" integrity="sha384-{{ script.sha384 }}"></script>
{% endfor %}
{% endif %}
{% endblock %}
{% block footer %}