portal/source/index.html.php

94 lines
1.6 KiB
PHP
Raw Normal View History

2024-06-01 18:53:24 +02:00
<?php
2024-06-02 11:56:52 +02:00
/**
*/
2024-06-01 18:53:24 +02:00
function string_coin(
string $template,
array $arguments
) : string
{
$result = $template;
foreach ($arguments as $key => $value) {
$result = \str_replace(\sprintf('{{%s}}', $key), $value, $result);
}
return $result;
}
2024-06-02 11:56:52 +02:00
/**
*/
2024-06-01 18:53:24 +02:00
function template_render_by_name(
string $template_name,
array $arguments
) : string
{
return string_coin(
2024-06-02 11:56:52 +02:00
\file_get_contents(
string_coin(
'{{directory}}/templates/{{name}}.html.tpl',
[
'directory' => __DIR__,
'name' => $template_name,
]
)
),
2024-06-01 18:53:24 +02:00
$arguments
);
}
2024-06-02 11:56:52 +02:00
/**
*/
2024-06-01 18:53:24 +02:00
function main(
) : void
{
2024-07-30 10:01:39 +02:00
$data_raw = \json_decode(
2024-06-02 11:56:52 +02:00
\file_get_contents(__DIR__ . '/data.json'),
2024-06-01 18:53:24 +02:00
true
);
2024-07-30 10:01:39 +02:00
$data = [
'title' => ($data_raw['title'] ?? 'Portal'),
'entries' => \array_map(
fn($entry_raw) => [
'active' => ($entry_raw['active'] ?? true),
'target' => ($entry_raw['target'] ?? '#'),
'label' => ($entry_raw['label'] ?? ''),
'image' => ($entry_raw['image'] ?? ''),
'open_in_new_tab' => ($entry_raw['open_in_new_tab'] ?? false),
],
($data_raw['entries'] ?? []),
),
];
2024-06-01 18:53:24 +02:00
print(
template_render_by_name(
'main',
[
'title' => $data['title'],
'entries' => \implode(
"\n",
\array_map(
fn($entry) => template_render_by_name(
'entry',
[
2024-07-30 10:01:39 +02:00
'a_href' => $entry['target'],
'a_target' => ($entry['open_in_new_tab'] ? '_blank' : '_self'),
'img_src' => $entry['image'],
'img_alt' => $entry['label'],
2024-06-01 18:53:24 +02:00
'label' => $entry['label'],
]
),
2024-06-02 11:56:52 +02:00
\array_values(\array_filter(
$data['entries'],
2024-07-30 10:01:39 +02:00
fn($entry) => $entry['active']
2024-06-02 11:56:52 +02:00
))
2024-06-01 18:53:24 +02:00
)
),
]
)
);
}
main();
?>