4 * Runtime Extension Capture
7 * @subpackage PluginsInternal
10 class Smarty_Internal_Runtime_Capture
13 * Flag that this instance will not be cached
17 public $isPrivateExtension = true;
20 * Stack of capture parameter
24 private $captureStack = array();
27 * Current open capture sections
31 private $captureCount = 0;
38 private $countStack = array();
45 private $namedBuffer = array();
48 * Flag if callbacks are registered
52 private $isRegistered = false;
55 * Open capture section
57 * @param \Smarty_Internal_Template $_template
58 * @param string $buffer capture name
59 * @param string $assign variable name
60 * @param string $append variable name
62 public function open(Smarty_Internal_Template $_template, $buffer, $assign, $append)
64 if (!$this->isRegistered) {
65 $this->register($_template);
67 $this->captureStack[] = array($buffer, $assign, $append);
68 $this->captureCount ++;
73 * Register callbacks in template class
75 * @param \Smarty_Internal_Template $_template
77 private function register(Smarty_Internal_Template $_template)
79 $_template->startRenderCallbacks[] = array($this, 'startRender');
80 $_template->endRenderCallbacks[] = array($this, 'endRender');
81 $this->startRender($_template);
82 $this->isRegistered = true;
86 * Start render callback
88 * @param \Smarty_Internal_Template $_template
90 public function startRender(Smarty_Internal_Template $_template)
92 $this->countStack[] = $this->captureCount;
93 $this->captureCount = 0;
97 * Close capture section
99 * @param \Smarty_Internal_Template $_template
101 * @throws \SmartyException
103 public function close(Smarty_Internal_Template $_template)
105 if ($this->captureCount) {
106 list($buffer, $assign, $append) = array_pop($this->captureStack);
107 $this->captureCount --;
108 if (isset($assign)) {
109 $_template->assign($assign, ob_get_contents());
111 if (isset($append)) {
112 $_template->append($append, ob_get_contents());
114 $this->namedBuffer[ $buffer ] = ob_get_clean();
116 $this->error($_template);
121 * Error exception on not matching {capture}{/capture}
123 * @param \Smarty_Internal_Template $_template
125 * @throws \SmartyException
127 public function error(Smarty_Internal_Template $_template)
129 throw new SmartyException("Not matching {capture}{/capture} in \"{$_template->template_resource}\"");
133 * Return content of named capture buffer
135 * @param \Smarty_Internal_Template $_template
140 public function getBuffer(Smarty_Internal_Template $_template, $name)
142 return isset($this->namedBuffer[ $name ]) ? $this->namedBuffer[ $name ] : null;
146 * End render callback
148 * @param \Smarty_Internal_Template $_template
150 * @throws \SmartyException
152 public function endRender(Smarty_Internal_Template $_template)
154 if ($this->captureCount) {
155 $this->error($_template);
157 $this->captureCount = array_pop($this->countStack);