File size: 2,119 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace Maveriks\Pattern\Mvc;

class PhtmlView extends View
{
    public static $blocks = array();

    private static $instance;
    private static $extend = '';

    public function __construct($tpl = '')
    {
        parent::__construct($tpl);
    }

    //Wrapped

    public function render()
    {
        self::$instance = $this;
        $tplFile = $this->getTpl();

        if (empty($tplFile)) {
            throw new \Exception('ERROR: template file was not set!');
        }

        foreach ($this->getTemplateDir() as $dir) {
            if (file_exists($dir . DIRECTORY_SEPARATOR . $tplFile)) {
                $tplFile = $dir . DIRECTORY_SEPARATOR . $tplFile;
                break;
            }
        }

        if (! file_exists($tplFile)) {
            throw new \Exception('ERROR: template file '.$tplFile.' does not exist!');
        }

        extract($this->data);

        include $tplFile;

        if (! empty(self::$extend)) {
            $tplFile = ''; //$this->getTemplateDir() . DIRECTORY_SEPARATOR . self::$extend . '.phtml';

            foreach ($this->getTemplateDir() as $dir) {
                if (file_exists($dir . DIRECTORY_SEPARATOR . self::$extend . '.phtml')) {
                    $tplFile = $dir . DIRECTORY_SEPARATOR . self::$extend . '.phtml';
                    break;
                }
            }

            if (! file_exists($tplFile)) {
                throw new \Exception('ERROR: Layout template file '.$tplFile.' does not exist!');
            }

            extract($this->data);

            include $tplFile;
        }
    }

    static public function extend($name)
    {
        self::$extend = $name;
    }

    static public function block($name, $default = '')
    {
        if (is_string($default)) {
            if (array_key_exists($name, self::$blocks)) {
                $callback = self::$blocks[$name];
                $callback();
            } else {
                return $default;
            }

            return;
        } elseif ($default instanceof \Closure) {
            self::$blocks[$name] = $default;
        }
    }
}