diff options
| author | winter | 2024-12-23 18:20:25 +0000 |
|---|---|---|
| committer | winter | 2024-12-23 18:20:25 +0000 |
| commit | bccad1118364427ea5c97c08d132fd128368760b (patch) | |
| tree | 449a4f5b36309e1e80694718f72ebc6e136e0140 /misc/render_template.php | |
| parent | 094b1d010253f1a9a114303d823739e513834742 (diff) | |
make a crummy template system and use it for global timeline
Diffstat (limited to 'misc/render_template.php')
| -rw-r--r-- | misc/render_template.php | 48 |
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 |
