Passed
Push — main ( 1fab42...a42753 )
by Nelson
01:31
created

Template::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Pin\Libs;
4
5
/**
6
 * Template
7
 * Clase para renderizar vistas dentro de una plantilla
8
 * @author nelson rojas
9
 */
10
class Template 
11
{
12
	/**
13
	 * @var string
14
	 */
15
	private $_template = 'default';
16
17
	/**
18
	 * @var string
19
	 */
20
	private $_title = '';
21
22
	/**
23
	 * @var array
24
	 */
25
	private $_properties = [];
26
27
	/**
28
	 * Método para setear propiedades
29
	 * @param string $prop
30
	 * @param mixed $value
31
	 */
32
	public function set($prop, $value)
33
	{
34
		$this->_properties[$prop] = $value;
35
	}
36
37
38
	/**
39
	 * Método para obtener propiedades
40
	 * @param string $prop
41
	 * @return mixed|null
42
	 */
43
	public function get($prop)
44
	{
45
		return isset($this->_properties[$prop]) ? $this->_properties[$prop] : null;
46
	}
47
48
	/**
49
	 * Permite asignar el template manualmente
50
	 * @param string $template
51
	 */
52
	public function setTemplate($template)
53
	{
54
		$this->_template = $template;
55
	}
56
57
	/**
58
	 * Permite asignar el title manualmente
59
	 * @param string $title
60
	 */
61
	public function setTitle($title)
62
	{
63
		$this->_title = $title;
64
	}
65
66
67
68
	/**
69
	 * Permite renderizar una vista dentro de la plantilla
70
	 * @param string $view
71
	 */
72
	public function render($view)
73
	{
74
		
75
		
76
		$template_file = APP_PATH . 'views' . DS . '_shared' . DS . 'templates' . DS . $this->_template . '.phtml';
77
		
78
		ob_start();
79
		\Pin\Libs\Load::view($view, $this->_properties);
80
		$yield = ob_get_clean();
81
		if (file_exists($template_file)) {
82
			$title = $this->_title;
83
			include $template_file;
84
		} else {
85
			throw new \Exception("Archivo de template no encontrado $template_file", 1);
86
		}
87
	}
88
}
89