_classes/Template.php
39 lines · 964 bytes
1<?php
2
3
4class Template
5{
6 // Template placeholders, in the order they are filled in.
7 private const PLACEHOLDERS = [
8 'Page_Title',
9 'Page_Description',
10 'Canonical_URL',
11 'Page_Extras',
12 'Content_Column',
13 'User_Comments',
14 'Current_Year',
15 ];
16
17 private array $fields;
18
19 // $fields maps each {Placeholder} in _templates/template.html to its value.
20 public function __construct(array $fields)
21 {
22 $this->fields = $fields + ['Current_Year' => date("Y")];
23 }
24
25 public function echoTemplate()
26 {
27 // Get the template file
28 $templateFile = '_templates/template.html';
29 $page = file_get_contents($templateFile);
30
31 // Replace the template variables
32 foreach (self::PLACEHOLDERS as $name) {
33 $page = str_replace('{' . $name . '}', $this->fields[$name], $page);
34 }
35
36 // Echo the filled-out template
37 echo $page;
38 }
39}