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

View File

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

View File

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

View File

@ -20,10 +20,20 @@
{% block styles %} {% block styles %}
{{ encore_entry_link_tags('page') }} {{ 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 %} {% endblock %}
{% block scripts %} {% block scripts %}
{{ encore_entry_script_tags('page') }} {{ 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 %} {% endblock %}
{% block footer %} {% block footer %}