Passed
Branch main (efe307)
by Nelson
01:25
created

Template::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Template
5
 * Clase para renderizar vistas dentro de una plantilla
6
 * @author nelson rojas
7
 */
8
class Template 
9
{
10
	/**
11
	 * @var string
12
	 */
13
	private $_template = 'default';
14
15
	/**
16
	 * @var array
17
	 */
18
	private $_properties = [];
19
20
	/**
21
	 * Método mágico para setear propiedades
22
	 * @param string $prop
23
	 * @param mixed $value
24
	 */
25
	public function __set($prop, $value)
26
	{
27
		$this->_properties[$prop] = $value;
28
	}
29
30
31
	/**
32
	 * Método mágico para obtener propiedades
33
	 * @param string $prop
34
	 * @return mixed|null
35
	 */
36
	public function __get($prop)
37
	{
38
		return isset($this->_properties[$prop]) ? $this->_properties[$prop] : null;
39
	}
40
41
	/**
42
	 * Permite setear el template manualmente
43
	 * @param string $template
44
	 */
45
	public function setTemplate($template)
46
	{
47
		$this->_template = $template;
48
	}
49
50
51
	/**
52
	 * Permite renderizar una vista dentro de la plantilla
53
	 * @param string $view
54
	 */
55
	public function render($view)
56
	{
57
		$template_file = PIN_PATH . 'templates' . DS . $this->_template . '.phtml';
58
		
59
		ob_start();
60
		load_view($view, $this->_properties);
61
		$yield = ob_get_clean();
62
		if (file_exists($template_file)) {
63
			include $template_file;
64
		} else {
65
			throw new Exception("Archivo de template no encontrado $template_file", 1);
66
		}
67
	}
68
}