view::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * © 2018 - Phoponent
5
 * Author: Nicolas Choquet
6
 * Email: [email protected]
7
 * LICENSE GPL ( GNU General Public License )
8
 */
9
10
namespace phoponent\framework\traits;
11
12
use phoponent\framework\static_classe\xphp;
13
14
trait view {
15
    private $template = '';
16
    protected $vars = [];
17
    protected $component = '';
18
19
    public function __construct($path) {
20
    	$view_name = explode('\\', get_class($this))[count(explode('\\', get_class($this)))-1];
21
        if(is_file("{$path}/src/{$view_name}.view.html")) {
22
            $this->template = file_get_contents("{$path}/src/{$view_name}.view.html");
23
        }
24
    }
25
26
    public function set_parent($parent_class) {
27
        if(strstr(__CLASS__, 'custom')) {
28
            $this->set_vars(['parent' => $parent_class->get_template()]);
29
        }
30
    }
31
32
    public function set_vars($vars) {
33
        foreach ($vars as $var => $value) {
34
            $this->vars[$var] = $value;
35
        }
36
        return $this;
37
    }
38
39
    public function get_template():string {
40
        return $this->template;
41
    }
42
43
    public function before_render() {}
44
45
	protected function render_end(&$template) {
46
		xphp::parse_template_content($template);
47
	}
48
49
    public function render():string {
50
        $this->before_render();
51
        $template = $this->get_template();
52
        foreach ($this->vars as $var => $value) {
53
            $template = str_replace("@{$var}@", $value, $template);
54
        }
55
        $this->render_end($template);
56
        return $template;
57
    }
58
}