summaryrefslogtreecommitdiffstats
path: root/core/namespace/Template/Template.php
blob: 0c68f92bcd7abf3e83da0642cd1fe167e4f30598 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace Template;

class Template {
	private $filename   = '';
	private $parameters = [];

	#===============================================================================
	# Create template instance
	#===============================================================================
	public function __construct($filename) {
		$this->filename = $filename;

		if(!file_exists($filename)) {
			throw new Exception("Template {$filename} does not exists.");
		}
	}

	#===============================================================================
	# Set value to array path
	#===============================================================================
	public function set($name, $value) {
		if(!is_array($name)) {
			return $this->parameters[$name] = $value;
		}

		$current = &$this->parameters;

		foreach($name as $path) {
			if(!isset($current[$path])) {
				$current[$path] = [];
			}
			$current = &$current[$path];
		}

		return $current = $value;
	}

	#===============================================================================
	# Add value as item to array path
	#===============================================================================
	public function add($paths, $value) {
		if(!is_array($paths)) {
			return $this->parameters[$paths][] = $value;
		}

		$current = &$this->parameters;

		foreach($paths as $path) {
			if(!isset($current[$path])) {
				$current[$path] = [];
			}
			$current = &$current[$path];
		}

		return $current[] = $value;
	}

	#===============================================================================
	# Return parsed template content
	#===============================================================================
	public function __toString() {
		foreach($this->parameters as $name => $value) {
			${$name} = $value;
		}

		ob_start();
		require $this->filename;
		return ob_get_clean();
	}
}
?>