dimaslanjaka /
universal-framework
| 1 | <?php |
||
| 2 | //Here I'm making the class. |
||
| 3 | class templateParse |
||
| 4 | { |
||
| 5 | //This is a var that is stored inside the class. |
||
| 6 | //It is private, meaning that only the class can access it. |
||
| 7 | //It holds the HTML from the template. |
||
| 8 | private $output; |
||
| 9 | |||
| 10 | //This is the contructer, meaing that the code will execute when the class is initiated. |
||
| 11 | //What it does, is checking if the file exists, if it does, it stores it inside the output variable. |
||
| 12 | //If it does not, it throws an exception that says that the file cannot be found. |
||
| 13 | function __construct($file = "default_template.html") |
||
| 14 | { |
||
| 15 | if (file_exists($file)) { |
||
| 16 | $this->output = file_get_contents($file); |
||
| 17 | } else { |
||
| 18 | throw new Exception('Error: ' . $file . ' not found'); |
||
| 19 | } |
||
| 20 | } |
||
| 21 | |||
| 22 | //In this function I start output buffering. This is used so the code that's in there, does not get send to the script. |
||
| 23 | //The function includes the input and get's the content and returns it whilst it turns off output buffering. |
||
| 24 | //This function is private cause it is called by another function inside the class. |
||
| 25 | private function parseFile($file) |
||
| 26 | { |
||
| 27 | ob_start(); |
||
| 28 | include($file); |
||
| 29 | $content = ob_get_contents(); |
||
| 30 | ob_end_clean(); |
||
| 31 | return $content; |
||
| 32 | } |
||
| 33 | |||
| 34 | //This function takes an array and checks how many things are inside of it. If it is above 0 it ocntinues |
||
| 35 | //if it is not, it throws an exception to display an error and does nothing. |
||
| 36 | //Once the array passes, the function does an foreach for each element inside of the array. |
||
| 37 | //First it checks if value of the array is a file that exists, if it is not, it just uses the regular value. |
||
| 38 | //If it is a file, it uses that files content. |
||
| 39 | //Second it goes through the output and replaces the tags inside of the tempalte (the {header} for example. Then saves it. |
||
| 40 | function ParseTemplate($tags = array()) |
||
|
0 ignored issues
–
show
|
|||
| 41 | { |
||
| 42 | if (count($tags) > 0) { |
||
| 43 | foreach ($tags as $tags => $data) { |
||
| 44 | $data = (file_exists($data)) ? $this->parseFile($data) : $data; |
||
| 45 | if (strpos($this->output, "{$tags}") !== false) { |
||
| 46 | $this->output = str_replace('{' . $tags . '}', $data, $this->output); |
||
| 47 | } |
||
| 48 | if (strpos($this->output, "<!--$tags-->") !== false) { |
||
| 49 | $this->output = str_replace("<!--$tags-->", $data, $this->output); |
||
| 50 | } |
||
| 51 | if (strpos($this->output, "/*$tags*/") !== false) { |
||
| 52 | $this->output = str_replace("/*$tags*/", $data, $this->output); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | } else { |
||
| 56 | throw new Exception('Error: No tags were provided for replacement'); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | /** |
||
| 60 | * This function just returns the output for display. |
||
| 61 | * |
||
| 62 | * @return string|false |
||
| 63 | */ |
||
| 64 | function display() |
||
|
0 ignored issues
–
show
|
|||
| 65 | { |
||
| 66 | return $this->output; |
||
| 67 | } |
||
| 68 | } |
||
| 69 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.