aboutsummaryrefslogtreecommitdiffhomepage
path: root/misc/render_template.php
diff options
context:
space:
mode:
Diffstat (limited to 'misc/render_template.php')
-rw-r--r--misc/render_template.php48
1 files changed, 48 insertions, 0 deletions
diff --git a/misc/render_template.php b/misc/render_template.php
new file mode 100644
index 0000000..1ee349d
--- /dev/null
+++ b/misc/render_template.php
@@ -0,0 +1,48 @@
+<?php
+
+/*
+here is a horrible example of php jank.
+TODO: anything better than this
+i'm terribly sorry for future me / anyone else who has to deal with this
+*/
+
+$__spooky_magical_template_global_state = [];
+
+function render_template(string $templateName, array $args = []) {
+ global $__spooky_magical_template_global_state;
+ $__spooky_magical_template_global_state['slotParentArgs'] = $__spooky_magical_template_global_state['slotArgs'] ?? null;
+ $__spooky_magical_template_global_state['slotArgs'] = $args;
+ foreach ($args as $key => $value) {
+ ${$key} = $value;
+ }
+
+ if (!function_exists('slot')) {
+ function slot() {
+ global $__spooky_magical_template_global_state;
+ if (isset($__spooky_magical_template_global_state['slotParentArgs'])) {
+ $parentArgs = $__spooky_magical_template_global_state['slotParentArgs'];
+ foreach ($parentArgs as $key => $value) {
+ global ${$key};
+ ${$key} = $value;
+ }
+ }
+ ($__spooky_magical_template_global_state['slotContent'] ?? function () {})();
+ if (isset($parentArgs)) {
+ foreach ($parentArgs as $key => $value) {
+ unset(${$key});
+ }
+ }
+ }
+ function call_template(string $templateName, array $args = [], callable $slotContent = null) {
+ global $__spooky_magical_template_global_state;
+ $__spooky_magical_template_global_state['slotContent'] = $slotContent;
+ render_template($templateName, $args);
+ }
+ }
+
+ require __DIR__ . '/../templates/' . $templateName . '.php';
+
+ foreach ($args as $key => $value) {
+ unset(${$key});
+ }
+} \ No newline at end of file