Layout.php
3.16 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* Created by PhpStorm.
* User: liuziyang
* Date: 14-1-12
* Time: 16:32
*/
namespace Plugin;
use Yaf\View_Interface;
use Yaf\View;
use Yaf\Application;
class Layout implements View_Interface
{
public $breadcrumb = array();
public $engine;
protected $options = array();
protected $layout_path;
protected $layout;
protected $content;
protected $tpl_vars = array();
protected $tpl_dir;
public function __construct($path, $options = array())
{
$this->layout_path = $path;
$this->options = $options;
}
protected function engine()
{
$this->engine = $this->engine ?: new View\Simple(
$this->tpl_dir,
$this->options
);
return $this->engine;
}
public function setScriptPath($path)
{
if (is_readable($path)) {
$this->tpl_dir = $path;
$this->engine()->setScriptPath($path);
$this->layout_path = $path . "/../layouts";
return true;
}
throw new Exception("Invalid path: {$path}");
}
public function getScriptPath()
{
return $this->engine()->getScriptPath();
}
public function setLayout($name)
{
$this->layout = $name;
}
public function getLayout()
{
return $this->layout;
}
public function setLayoutPath($path)
{
$this->layout_path = $path;
return $this;
}
public function getLayoutPath()
{
$config = Application::app()->getConfig()->get('application');
return $this->layout_path . "/" . $this->layout . ".{$config->view->ext}";
}
public function __set($name, $value)
{
$this->assign($name, $value);
}
public function __isset($name)
{
return (null !== $this->engine()->$name);
}
public function __unset($name)
{
$this->engine()->clear($name);
}
public function assign($name, $value = null)
{
$this->tpl_vars[$name] = $value;
$this->engine()->assign($name, $value);
}
public function assignRef($name, &$value)
{
$this->tpl_vars[$name] = $value;
$this->engine()->assignRef($name, $value);
}
public function clearVars($name)
{
$this->tpl_vars = array();
$this->engine()->clear($name);
}
public function render($tpl, $tpl_vars = array())
{
$tpl_vars = array_merge($this->tpl_vars, $tpl_vars);
$this->content = $this->engine()->render($tpl, $tpl_vars);
if (null == $this->layout) {
return $this->content;
}
$ref = new \ReflectionClass($this->engine());
$prop = $ref->getProperty('_tpl_vars');
$prop->setAccessible(true);
$view_vars = $prop->getValue($this->engine());
$tpl_vars = array_merge($tpl_vars, $view_vars);
$tpl_vars['content'] = $this->content;
$this->engine()->assign('breadcrumb', $this->breadcrumb);
return $this->engine()->render(
$this->getLayoutPath(),
$tpl_vars
);
}
public function display($tpl, $tpl_vars = array())
{
echo $this->render($tpl, $tpl_vars);
}
}