Completed
Push — master ( ead358...de8c68 )
by Askupa
01:58
created

Template::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 9.4285
1
<?php
2
3
namespace Amarkal\UI;
4
5
/**
6
 * Templates are a combination of markup and logic. Use templates to maintain
7
 * separation of concepts.
8
 */
9
class Template
10
{
11
    /**
12
     * An associative array holding the model to be used by this controller
13
     * @var array 
14
     */
15
    protected $model;
16
    
17
    /**
18
     * @var string The path to the template file
19
     */
20
    protected $template;
21
22
    /**
23
     * Constructor
24
     * 
25
     * @param array $model
26
     * @param string $template_path
27
     */
28
    public function __construct( array $model = array(), $template_path = null ) 
29
    {
30
        $this->set_model( $model );
31
        $this->template = $template_path;
32
    }
33
    
34
    /**
35
     * Get model argument value by name.
36
     * 
37
     * @param string $name The argument's name.
38
     * 
39
     * @return mixed the argument's value.
40
     */
41
    public function __get( $name ) 
42
    {
43
        if( isset( $this->model[$name] ) )
44
        {
45
            return $this->model[$name];
46
        }
47
    }
48
    
49
    /**
50
     * Set model argument by name.
51
     * 
52
     * @param string $name The argument's name.
53
     * @param mixed $value The value to set.
54
     * 
55
     * @return mixed the settings' argument value.
56
     */
57
    public function __set( $name, $value )
58
    {
59
        $this->model[$name] = $value;
60
    }
61
    
62
    /**
63
     * Get the current component model data.
64
     * 
65
     * @return array
66
     */
67
    public function get_model()
68
    {
69
        return $this->model;
70
    }
71
    
72
    /**
73
     * Set the model data for this component.
74
     * 
75
     * @return array
76
     */
77
    public function set_model( $model )
78
    {
79
        $this->model = $model;
80
    }
81
    
82
    /**
83
     * Get the full path to the template file.
84
     * 
85
     * @return string The full path.
86
     */
87
    public function get_template_path()
88
    {
89
        return $this->template;
90
    }
91
    
92
    /**
93
     * Render the template with the local properties.
94
     * 
95
     * @return string The rendered template.
96
     * @throws TemplateNotFoundException Thrown if the template file cannot be found.
97
     */
98
    public function render( $echo = false )
99
    {
100
        $rendered_html = '';
101
        
102
        if( file_exists( $this->get_template_path() ) ) 
103
        {
104
            ob_start();
105
            include( $this->get_template_path() );
106
            $rendered_html = ob_get_clean();
107
        } 
108
        else 
109
        {
110
            throw new \RuntimeException( "Error: cannot render HTML, template file not found at " . $this->get_template_path() );
111
        }
112
        
113
        if( !$echo )
114
        {
115
            return $rendered_html;
116
        }
117
        echo $rendered_html;
118
    }
119
}