1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Latte\Macros;
9:
10: use Latte,
11: Latte\MacroNode,
12: Latte\PhpWriter,
13: Latte\CompileException,
14: Latte\RuntimeException;
15:
16:
17: 18: 19: 20: 21:
22: class BlockMacros extends MacroSet
23: {
24:
25: private $namedBlocks = array();
26:
27:
28: private $extends;
29:
30:
31: public static function install(Latte\Compiler $compiler)
32: {
33: $me = new static($compiler);
34: $me->addMacro('include', array($me, 'macroInclude'));
35: $me->addMacro('includeblock', array($me, 'macroIncludeBlock'));
36: $me->addMacro('extends', array($me, 'macroExtends'));
37: $me->addMacro('layout', array($me, 'macroExtends'));
38: $me->addMacro('block', array($me, 'macroBlock'), array($me, 'macroBlockEnd'));
39: $me->addMacro('define', array($me, 'macroBlock'), array($me, 'macroBlockEnd'));
40: $me->addMacro('snippet', array($me, 'macroBlock'), array($me, 'macroBlockEnd'));
41: $me->addMacro('snippetArea', array($me, 'macroBlock'), array($me, 'macroBlockEnd'));
42: $me->addMacro('ifset', array($me, 'macroIfset'), '}');
43: $me->addMacro('elseifset', array($me, 'macroIfset'), '}');
44: }
45:
46:
47: 48: 49: 50:
51: public function initialize()
52: {
53: $this->namedBlocks = array();
54: $this->extends = NULL;
55: }
56:
57:
58: 59: 60: 61:
62: public function finalize()
63: {
64:
65: $last = $this->getCompiler()->getMacroNode();
66: if ($last && $last->name === 'block') {
67: $this->getCompiler()->closeMacro($last->name);
68: }
69:
70: $epilog = $prolog = array();
71:
72: if ($this->namedBlocks) {
73: foreach ($this->namedBlocks as $name => $code) {
74: $func = '_lb' . substr(md5($this->getCompiler()->getTemplateId() . $name), 0, 10) . '_' . preg_replace('#[^a-z0-9_]#i', '_', $name);
75: $snippet = $name[0] === '_';
76: $prolog[] = "//\n// block $name\n//\n"
77: . "if (!function_exists(\$_b->blocks[" . var_export($name, TRUE) . "][] = '$func')) { "
78: . "function $func(\$_b, \$_args) { foreach (\$_args as \$__k => \$__v) \$\$__k = \$__v"
79: . ($snippet ? '; $_control->redrawControl(' . var_export(substr($name, 1), TRUE) . ', FALSE)' : '')
80: . "\n?>$code<?php\n}}";
81: }
82: $prolog[] = "//\n// end of blocks\n//";
83: }
84:
85: if ($this->namedBlocks || $this->extends) {
86: $prolog[] = '// template extending';
87:
88: $prolog[] = '$_l->extends = '
89: . ($this->extends ? $this->extends : 'empty($_g->extended) && isset($_control) && $_control instanceof Nette\Application\UI\Presenter ? $_control->findLayoutTemplateFile() : NULL')
90: . '; $_g->extended = TRUE;';
91:
92: $prolog[] = 'if ($_l->extends) { ' . ($this->namedBlocks ? 'ob_start();' : 'return $template->renderChildTemplate($_l->extends, get_defined_vars());') . '}';
93: }
94:
95: return array(implode("\n\n", $prolog), implode("\n", $epilog));
96: }
97:
98:
99:
100:
101:
102: 103: 104:
105: public function macroInclude(MacroNode $node, PhpWriter $writer)
106: {
107: $destination = $node->tokenizer->fetchWord();
108: if (!preg_match('~#|[\w-]+\z~A', $destination)) {
109: return FALSE;
110: }
111:
112: $destination = ltrim($destination, '#');
113: $parent = $destination === 'parent';
114: if ($destination === 'parent' || $destination === 'this') {
115: for ($item = $node->parentNode; $item && $item->name !== 'block' && !isset($item->data->name); $item = $item->parentNode);
116: if (!$item) {
117: throw new CompileException("Cannot include $destination block outside of any block.");
118: }
119: $destination = $item->data->name;
120: }
121:
122: $name = strpos($destination, '$') === FALSE ? var_export($destination, TRUE) : $destination;
123: if (isset($this->namedBlocks[$destination]) && !$parent) {
124: $cmd = "call_user_func(reset(\$_b->blocks[$name]), \$_b, %node.array? + get_defined_vars())";
125: } else {
126: $cmd = 'Latte\Macros\BlockMacrosRuntime::callBlock' . ($parent ? 'Parent' : '') . "(\$_b, $name, %node.array? + " . ($parent ? 'get_defined_vars' : '$template->getParameters') . '())';
127: }
128:
129: if ($node->modifiers) {
130: return $writer->write("ob_start(); $cmd; echo %modify(ob_get_clean())");
131: } else {
132: return $writer->write($cmd);
133: }
134: }
135:
136:
137: 138: 139:
140: public function macroIncludeBlock(MacroNode $node, PhpWriter $writer)
141: {
142: return $writer->write('ob_start(); $_b->templates[%var]->renderChildTemplate(%node.word, %node.array? + get_defined_vars()); echo rtrim(ob_get_clean())',
143: $this->getCompiler()->getTemplateId());
144: }
145:
146:
147: 148: 149:
150: public function macroExtends(MacroNode $node, PhpWriter $writer)
151: {
152: if (!$node->args) {
153: throw new CompileException("Missing destination in {{$node->name}}");
154: }
155: if (!empty($node->parentNode)) {
156: throw new CompileException("{{$node->name}} must be placed outside any macro.");
157: }
158: if ($this->extends !== NULL) {
159: throw new CompileException("Multiple {{$node->name}} declarations are not allowed.");
160: }
161: if ($node->args === 'none') {
162: $this->extends = 'FALSE';
163: } elseif ($node->args === 'auto') {
164: $this->extends = '$_presenter->findLayoutTemplateFile()';
165: } else {
166: $this->extends = $writer->write('%node.word%node.args');
167: }
168: return;
169: }
170:
171:
172: 173: 174: 175: 176: 177:
178: public function macroBlock(MacroNode $node, PhpWriter $writer)
179: {
180: $name = $node->tokenizer->fetchWord();
181:
182: if ($node->name === '#') {
183: trigger_error('Shortcut {#block} is deprecated.', E_USER_DEPRECATED);
184:
185: } elseif ($node->name === 'block' && $name === FALSE) {
186: return $node->modifiers === '' ? '' : 'ob_start()';
187: }
188:
189: $node->data->name = $name = ltrim($name, '#');
190: if ($name == NULL) {
191: if ($node->name === 'define') {
192: throw new CompileException('Missing block name.');
193: }
194:
195: } elseif (strpos($name, '$') !== FALSE) {
196: if ($node->name === 'snippet') {
197: for ($parent = $node->parentNode; $parent && !($parent->name === 'snippet' || $parent->name === 'snippetArea'); $parent = $parent->parentNode);
198: if (!$parent) {
199: throw new CompileException('Dynamic snippets are allowed only inside static snippet/snippetArea.');
200: }
201: $parent->data->dynamic = TRUE;
202: $node->data->leave = TRUE;
203: $node->closingCode = "<?php \$_l->dynSnippets[\$_l->dynSnippetId] = ob_get_flush() ?>";
204:
205: if ($node->prefix) {
206: $node->attrCode = $writer->write("<?php echo ' id=\"' . (\$_l->dynSnippetId = \$_control->getSnippetId({$writer->formatWord($name)})) . '\"' ?>");
207: return $writer->write('ob_start()');
208: }
209: $tag = trim($node->tokenizer->fetchWord(), '<>');
210: $tag = $tag ? $tag : 'div';
211: $node->closingCode .= "\n</$tag>";
212: return $writer->write("?>\n<$tag id=\"<?php echo \$_l->dynSnippetId = \$_control->getSnippetId({$writer->formatWord($name)}) ?>\"><?php ob_start()");
213:
214: } else {
215: $node->data->leave = TRUE;
216: $fname = $writer->formatWord($name);
217: $node->closingCode = "<?php }} " . ($node->name === 'define' ? '' : "call_user_func(reset(\$_b->blocks[$fname]), \$_b, get_defined_vars())") . " ?>";
218: $func = '_lb' . substr(md5($this->getCompiler()->getTemplateId() . $name), 0, 10) . '_' . preg_replace('#[^a-z0-9_]#i', '_', $name);
219: return "\n\n//\n// block $name\n//\n"
220: . "if (!function_exists(\$_b->blocks[$fname]['{$this->getCompiler()->getTemplateId()}'] = '$func')) { "
221: . "function $func(\$_b, \$_args) { foreach (\$_args as \$__k => \$__v) \$\$__k = \$__v";
222: }
223: }
224:
225:
226: if ($node->name === 'snippet' || $node->name === 'snippetArea') {
227: if ($node->prefix && isset($node->htmlNode->attrs['id'])) {
228: throw new CompileException('Cannot combine HTML attribute id with n:snippet.');
229: }
230: $node->data->name = $name = '_' . $name;
231: }
232:
233: if (isset($this->namedBlocks[$name])) {
234: throw new CompileException("Cannot redeclare static {$node->name} '$name'");
235: }
236:
237: $prolog = $this->namedBlocks ? '' : "if (\$_l->extends) { ob_end_clean(); return \$template->renderChildTemplate(\$_l->extends, get_defined_vars()); }\n";
238: $this->namedBlocks[$name] = TRUE;
239:
240: $include = 'call_user_func(reset($_b->blocks[%var]), $_b, ' . (($node->name === 'snippet' || $node->name === 'snippetArea') ? '$template->getParameters()' : 'get_defined_vars()') . ')';
241: if ($node->modifiers) {
242: $include = "ob_start(); $include; echo %modify(ob_get_clean())";
243: }
244:
245: if ($node->name === 'snippet') {
246: if ($node->prefix) {
247: $node->attrCode = $writer->write('<?php echo \' id="\' . $_control->getSnippetId(%var) . \'"\' ?>', (string) substr($name, 1));
248: return $writer->write($prolog . $include, $name);
249: }
250: $tag = trim($node->tokenizer->fetchWord(), '<>');
251: $tag = $tag ? $tag : 'div';
252: return $writer->write("$prolog ?>\n<$tag id=\"<?php echo \$_control->getSnippetId(%var) ?>\"><?php $include ?>\n</$tag><?php ",
253: (string) substr($name, 1), $name
254: );
255:
256: } elseif ($node->name === 'define') {
257: return $prolog;
258:
259: } else {
260: return $writer->write($prolog . $include, $name);
261: }
262: }
263:
264:
265: 266: 267: 268: 269: 270:
271: public function macroBlockEnd(MacroNode $node, PhpWriter $writer)
272: {
273: if (isset($node->data->name)) {
274: if ($node->name === 'snippet' && $node->prefix === MacroNode::PREFIX_NONE
275: && preg_match('#^.*? n:\w+>\n?#s', $node->content, $m1) && preg_match('#[ \t]*<[^<]+\z#s', $node->content, $m2)
276: ) {
277: $node->openingCode = $m1[0] . $node->openingCode;
278: $node->content = substr($node->content, strlen($m1[0]), -strlen($m2[0]));
279: $node->closingCode .= $m2[0];
280: }
281:
282: if (empty($node->data->leave)) {
283: if ($node->name === 'snippetArea') {
284: $node->content = "<?php \$_control->snippetMode = isset(\$_snippetMode) && \$_snippetMode; ?>{$node->content}<?php \$_control->snippetMode = FALSE; ?>";
285: }
286: if (!empty($node->data->dynamic)) {
287: $node->content .= '<?php if (isset($_l->dynSnippets)) return $_l->dynSnippets; ?>';
288: }
289: if ($node->name === 'snippetArea') {
290: $node->content .= '<?php return FALSE; ?>';
291: }
292: $this->namedBlocks[$node->data->name] = $tmp = rtrim(ltrim($node->content, "\n"), " \t");
293: $node->content = substr_replace($node->content, $node->openingCode . "\n", strspn($node->content, "\n"), strlen($tmp));
294: $node->openingCode = '<?php ?>';
295: }
296:
297: } elseif ($node->modifiers) {
298: return $writer->write('echo %modify(ob_get_clean())');
299: }
300: }
301:
302:
303: 304: 305: 306:
307: public function macroIfset(MacroNode $node, PhpWriter $writer)
308: {
309: if (!preg_match('~#|[\w-]+\z~A', $node->args)) {
310: return FALSE;
311: }
312: $list = array();
313: while (($name = $node->tokenizer->fetchWord()) !== FALSE) {
314: $list[] = preg_match('~#|[\w-]+\z~A', $name)
315: ? '$_b->blocks["' . ltrim($name, '#') . '"]'
316: : $writer->formatArgs(new Latte\MacroTokens($name));
317: }
318: return ($node->name === 'elseifset' ? '} else' : '')
319: . 'if (isset(' . implode(', ', $list) . ')) {';
320: }
321:
322: }
323: