diff options
Diffstat (limited to 'php')
| -rw-r--r-- | php/app.php | 23 | ||||
| -rw-r--r-- | php/footer.php | 14 | ||||
| -rw-r--r-- | php/replace.php | 13 | ||||
| -rw-r--r-- | php/templates.php | 20 |
4 files changed, 70 insertions, 0 deletions
diff --git a/php/app.php b/php/app.php new file mode 100644 index 0000000..cde972d --- /dev/null +++ b/php/app.php @@ -0,0 +1,23 @@ +<?php + +ini_set('display_errors', 1); +error_reporting(E_ALL); + +define('ROOT_DIR', __DIR__ . '/../'); +define('HTML_DIR', __DIR__ . '/../html/'); +define('PHP_DIR', __DIR__ . '/../php/'); + +require_once PHP_DIR . 'templates.php'; + +$file = $_GET['file'] ?? 'index.html'; + +$components = [ + '{{HEADER}}' => HTML_DIR . 'header.html', + '{{CONTENT}}' => ROOT_DIR . $file, + '{{FOOTER}}' => PHP_DIR . 'footer.php' +]; + +$app_html = template_process(HTML_DIR . 'app.html', $components); + +echo $app_html; +?> diff --git a/php/footer.php b/php/footer.php new file mode 100644 index 0000000..4a32baf --- /dev/null +++ b/php/footer.php @@ -0,0 +1,14 @@ +<?php + +require_once PHP_DIR . 'replace.php'; + +$year = date('Y'); + +$targets = [ + '{{YEAR}}' => $year +]; + +$html = replace(HTML_DIR . 'footer.html', $targets); + +echo $html; +?> diff --git a/php/replace.php b/php/replace.php new file mode 100644 index 0000000..70fdd25 --- /dev/null +++ b/php/replace.php @@ -0,0 +1,13 @@ +<?php + +function replace(string $templatePath, array $replacements = []): string { + $html = file_get_contents($templatePath); + + foreach ($replacements as $placeholder => $replacementValue) { + $html = str_replace($placeholder, $replacementValue, $html); + } + + return $html; +} + +?> diff --git a/php/templates.php b/php/templates.php new file mode 100644 index 0000000..4ead3f8 --- /dev/null +++ b/php/templates.php @@ -0,0 +1,20 @@ +<?php + +function render_component(string $componentPath): string { + ob_start(); + include($componentPath); + return ob_get_clean(); +} + +function template_process(string $template, array $components = []): string { + $html = file_get_contents($template); + + foreach ($components as $placeholder => $componentPath) { + $injectedContent = render_component($componentPath); + $html = str_replace($placeholder, $injectedContent, $html); + } + + return $html; +} + +?> |
