1: <?php
2:
3: namespace PHPixie\Template\Renderer;
4:
5: class Runtime
6: {
7: protected $context;
8: protected $openBuffers = 0;
9:
10: public function __construct($context)
11: {
12: $this->context = $context;
13: }
14:
15: public function run()
16: {
17: extract($this->context->variables());
18:
19: try {
20: while(($template = $this->context->template()) !== null) {
21: ob_start();
22: $this->openBuffers++;
23:
24: include $this->resolve($template);
25:
26: $content = ob_get_clean();
27: $this->openBuffers--;
28:
29: $this->context->processRender($content);
30: }
31:
32: }catch(\Exception $exception) {
33: while($this->openBuffers > 0) {
34: ob_end_clean();
35: $this->openBuffers--;
36: }
37:
38: throw $exception;
39: }
40:
41: return $content;
42:
43: }
44:
45: protected function render($template, $data = array())
46: {
47: return $this->context->renderer()->render($template, $data);
48: }
49:
50: protected function resolve($template)
51: {
52: return $this->context->resolver()->resolve($template);
53: }
54:
55: protected function startBlock($name, $onlyIfNotExists = false, $prepend = false)
56: {
57: if($onlyIfNotExists && $this->context->blockExists($name)) {
58: return false;
59: }
60:
61: ob_start();
62: $this->openBuffers++;
63:
64: $this->context->startBlock($name, !$prepend);
65:
66: return true;
67: }
68:
69: protected function endBlock()
70: {
71: $content = ob_get_clean();
72: $this->openBuffers--;
73:
74: return $this->context->endBlock($content);
75: }
76:
77: protected function layout($name)
78: {
79: $this->context->setLayout($name);
80: }
81:
82: protected function childContent()
83: {
84: echo $this->context->childContent();
85: }
86:
87: protected function blockExists($name)
88: {
89: return $this->context->blockExists($name);
90: }
91:
92: protected function block($name)
93: {
94: echo $this->context->block($name);
95: }
96:
97: protected function extension($name)
98: {
99: return $this->context->extension($name);
100: }
101:
102: public function __call($name, $params)
103: {
104: return $this->context->callExtensionMethod($name, $params);
105: }
106:
107: protected function get($path, $default = null)
108: {
109: return $this->data()->get($path, $default);
110: }
111:
112: protected function set($path, $data)
113: {
114: $this->data()->set($path, $data);
115: }
116:
117: protected function remove($path)
118: {
119: $this->data()->remove($path);
120: }
121:
122: protected function data()
123: {
124: return $this->context->data();
125: }
126: }