Source for file LatteMacros.php
Documentation is available at LatteMacros.php
- 1: <?php
- 3: /**
- 4: * Nette Framework
- 5: *
- 6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
- 7: *
- 8: * This source file is subject to the "Nette license" that is bundled
- 9: * with this package in the file license.txt.
- 10: *
- 11: * For more information please see http://nettephp.com
- 12: *
- 18: */
- 26: /**
- 27: * Default macros for filter LatteFilter.
- 28: *
- 29: * - {$variable} with escaping
- 30: * - {!$variable} without escaping
- 31: * - {*comment*} will be removed
- 32: * - {=expression} echo with escaping
- 33: * - {!=expression} echo without escaping
- 34: * - {?expression} evaluate PHP statement
- 35: * - {_expression} echo translation with escaping
- 36: * - {!_expression} echo translation without escaping
- 37: * - {link destination ...} control link
- 38: * - {plink destination ...} presenter link
- 39: * - {if ?} ... {elseif ?} ... {else} ... {/if}
- 40: * - {ifset ?} ... {elseifset ?} ... {/if}
- 41: * - {for ?} ... {/for}
- 42: * - {foreach ?} ... {/foreach}
- 43: * - {include ?}
- 44: * - {cache ?} ... {/cache} cached block
- 45: * - {snippet ?} ... {/snippet ?} control snippet
- 46: * - {attr ?} HTML element attributes
- 47: * - {block|texy} ... {/block} block
- 48: * - {contentType ...} HTTP Content-Type header
- 49: * - {status ...} HTTP status
- 50: * - {capture ?} ... {/capture} capture block to parameter
- 51: * - {assign var => value} set template parameter
- 52: * - {default var => value} set default template parameter
- 53: * - {dump $var}
- 54: * - {debugbreak}
- 55: *
- 59: */
- 61: {
- 76: 'cache' => '<?php if ($_cb->foo = CachingHelper::create($_cb->key = md5(__FILE__) . __LINE__, $template->getFile(), array(%%))) { $_cb->caches[] = $_cb->foo ?>',
- 77: '/cache' => '<?php array_pop($_cb->caches)->save(); } if (!empty($_cb->caches)) end($_cb->caches)->addItem($_cb->key) ?>',
- 101: 'ifCurrent' => '<?php %:macroIfCurrent%; if ($presenter->getLastCreatedRequestFlag("current")): ?>',
- 110: 'dump' => '<?php Debug::consoleDump(%:macroDump%, "Template " . str_replace(Environment::getVariable("appDir"), "\xE2\x80\xA6", $template->getFile())) ?>',
- 144: /**#@+ @ignore internal block type */
- 148: /**#@-*/
- 152: /**
- 153: * Constructor.
- 154: */
- 156: {
- 158: }
- 162: /**
- 163: * Initializes parsing.
- 167: */
- 169: {
- 179: // remove comments
- 182: // snippets support (temporary solution)
- 186: $s
- 188: }
- 192: /**
- 193: * Finishes parsing.
- 196: */
- 198: {
- 199: // blocks closing check
- 205: }
- 207: // snippets support (temporary solution)
- 210: // extends support
- 215: . 'if ($_cb->extends) { ob_end_clean(); LatteMacros::includeTemplate($_cb->extends, get_defined_vars(), $template)->render(); }' . "\n";
- 216: }
- 218: // named blocks
- 222: $s = preg_replace_callback("#{block ($name)} \?>(.*)<\?php {/block $name}#sU", array($this, 'cbNamedBlocks'), $s);
- 223: }
- 225: }
- 227: // internal state holder
- 230: . "\$_cb = LatteMacros::initRuntime(\$template, " . var_export($this->extends, TRUE) . ", " . var_export($this->uniq, TRUE) . "); unset(\$_extends);\n"
- 232: }
- 236: /**
- 237: * Process {macro content | modifiers}
- 242: */
- 244: {
- 251: }
- 252: }
- 257: }
- 260: }
- 264: /**
- 265: * Callback for self::macro().
- 266: */
- 268: {
- 275: throw new InvalidStateException("Latte macro handler '$textual' is not " . ($able ? 'callable.' : 'valid PHP callback.'));
- 276: }
- 281: }
- 282: }
- 286: /**
- 287: * Process <n:tag attr> (experimental).
- 292: */
- 294: {
- 304: isset($knownTags[$name], $attrs[$knownTags[$name]]) ? $attrs[$knownTags[$name]] : substr(var_export($attrs, TRUE), 8, -1),
- 307: }
- 311: /**
- 312: * Process <tag n:attr> (experimental).
- 317: */
- 319: {
- 324: }
- 332: }
- 333: }
- 341: }
- 342: }
- 348: }
- 351: }
- 354: }
- 358: /********************* macros ****************d*g**/
- 362: /**
- 363: * {$var |modifiers}
- 364: */
- 366: {
- 368: }
- 372: /**
- 373: * {_$var |modifiers}
- 374: */
- 376: {
- 378: }
- 382: /**
- 383: * {syntax ...}
- 384: */
- 386: {
- 409: default:
- 411: }
- 412: }
- 416: /**
- 417: * {include ...}
- 418: */
- 419: private function macroInclude($content, $modifiers)
- 420: {
- 421: $destination = LatteFilter::fetchToken($content); // destination [,] [params]
- 422: $params = LatteFilter::formatArray($content) . ($content ? ' + ' : '');
- 424: if ($destination === NULL) {
- 425: throw new <a href="../Nette/InvalidStateException.html">InvalidStateException</a>("Missing destination in {include} on line {$this->filter->line}.");
- 427: } elseif ($destination[0] === '#') { // include #block
- 429: if (!preg_match('#^'.<a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::RE_IDENTIFIER.'$#', $destination)) {
- 430: throw new <a href="../Nette/InvalidStateException.html">InvalidStateException</a>("Included block name must be alphanumeric string, '$destination' given on line {$this->filter->line}.");
- 431: }
- 433: $parent = $destination === 'parent';
- 434: if ($destination === 'parent' || $destination === 'this') {
- 437: if (!$item) {
- 438: throw new <a href="../Nette/InvalidStateException.html">InvalidStateException</a>("Cannot include $destination block outside of any block on line {$this->filter->line}.");
- 439: }
- 440: $destination = $item[1];
- 441: }
- 442: $name = var_export($destination, TRUE);
- 443: $params .= 'get_defined_vars()';
- 444: $cmd = isset($this->namedBlocks[$destination]) && !$parent
- 447: return $modifiers
- 449: : $cmd;
- 451: } else { // include "file"
- 452: $destination = <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatString($destination);
- 453: $params .= '$template->getParams()';
- 454: return $modifiers
- 455: ? 'echo ' . <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatModifiers('LatteMacros::includeTemplate(' . $destination . ', ' . $params . ', $_cb->templates[' . var_export($this->uniq, TRUE) . '])->__toString(TRUE)', $modifiers)
- 456: : 'LatteMacros::includeTemplate(' . $destination . ', ' . $params . ', $_cb->templates[' . var_export($this->uniq, TRUE) . '])->render()';
- 457: }
- 458: }
- 462: /**
- 463: * {extends ...}
- 464: */
- 465: private function macroExtends($content)
- 466: {
- 467: $destination = <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::fetchToken($content); // destination
- 468: if ($destination === NULL) {
- 469: throw new <a href="../Nette/InvalidStateException.html">InvalidStateException</a>("Missing destination in {extends} on line {$this->filter->line}.");
- 470: }
- 471: if (!empty($this->blocks)) {
- 472: throw new InvalidStateException("{extends} must be placed outside any block; on line {$this->filter->line}.");
- 473: }
- 474: if ($this->extends !== NULL) {
- 475: throw new InvalidStateException("Multiple {extends} declarations are not allowed; on line {$this->filter->line}.");
- 476: }
- 477: $this->extends = $destination !== 'none';
- 478: return $this->extends ? '$_cb->extends = ' . <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatString($destination) : '';
- 479: }
- 483: /**
- 484: * {block ...}
- 485: */
- 486: private function macroBlock($content, $modifiers)
- 487: {
- 489: trigger_error("Capturing {block $content} is deprecated; use {capture $content} instead on line {$this->filter->line}.", E_USER_WARNING);
- 490: return $this->macroCapture($content, $modifiers);
- 491: }
- 493: $name = LatteFilter::fetchToken($content); // block [,] [params]
- 495: if ($name === NULL) { // anonymous block
- 496: $this->blocks[] = array(self::BLOCK_ANONYMOUS, NULL, $modifiers);
- 497: return $modifiers === '' ? '' : 'ob_start()';
- 499: } else { // #block
- 501: if (!preg_match('#^'.<a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::RE_IDENTIFIER.'$#', $name)) {
- 502: throw new <a href="../Nette/InvalidStateException.html">InvalidStateException</a>("Block name must be alphanumeric string, '$name' given on line {$this->filter->line}.");
- 504: } elseif (isset($this->namedBlocks[$name])) {
- 505: throw new InvalidStateException("Cannot redeclare block '$name'; on line {$this->filter->line}.");
- 506: }
- 508: $top = empty($this->blocks);
- 509: $this->namedBlocks[$name] = $name;
- 510: $this->blocks[] = array(self::BLOCK_NAMED, $name, '');
- 511: if (!$top) {
- 514: } elseif ($this->extends) {
- 517: } else {
- 518: return 'if (!$_cb->extends) { ' . $this->macroInclude('#' . $name, $modifiers) . "; } {block $name}";
- 519: }
- 520: }
- 521: }
- 525: /**
- 526: * {/block}
- 527: */
- 528: private function macroBlockEnd($content)
- 529: {
- 530: list($type, $name, $modifiers) = array_pop($this->blocks);
- 532: if ($type === self::BLOCK_CAPTURE) { // capture - back compatibility
- 533: $this->blocks[] = array($type, $name, $modifiers);
- 534: return $this->macroCaptureEnd($content);
- 535: }
- 537: if (($type !== self::BLOCK_NAMED && $type !== self::BLOCK_ANONYMOUS) || ($content && $content !== $name)) {
- 538: throw new InvalidStateException("Tag {/block $content} was not expected here on line {$this->filter->line}.");
- 540: } elseif ($type === self::BLOCK_NAMED) { // block
- 543: } else { // anonymous block
- 544: return $modifiers === '' ? '' : 'echo ' . <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatModifiers('ob_get_clean()', $modifiers);
- 545: }
- 546: }
- 550: /**
- 551: * {snippet ...}
- 552: */
- 553: private function macroSnippet($content)
- 554: {
- 555: $args = array('');
- 556: if ($snippet = <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::fetchToken($content)) { // [name [,]] [tag]
- 557: $args[] = <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatString($snippet);
- 558: }
- 559: if ($content) {
- 560: $args[] = <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatString($content);
- 561: }
- 563: }
- 567: /**
- 568: * {snippet ...}
- 569: */
- 570: private function macroSnippetEnd($content)
- 571: {
- 572: return 'array_pop($_cb->snippets)->finish(); } if (SnippetHelper::$outputAllowed) {';
- 573: }
- 577: /**
- 578: * {capture ...}
- 579: */
- 580: private function macroCapture($content, $modifiers)
- 581: {
- 582: $name = <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::fetchToken($content); // $variable
- 585: throw new <a href="../Nette/InvalidStateException.html">InvalidStateException</a>("Invalid capture block parameter '$name' on line {$this->filter->line}.");
- 586: }
- 588: $this->blocks[] = array(self::BLOCK_CAPTURE, $name, $modifiers);
- 589: return 'ob_start()';
- 590: }
- 594: /**
- 595: * {/capture}
- 596: */
- 597: private function macroCaptureEnd($content)
- 598: {
- 601: if ($type !== self::BLOCK_CAPTURE || ($content && $content !== $name)) {
- 602: throw new <a href="../Nette/InvalidStateException.html">InvalidStateException</a>("Tag {/capture $content} was not expected here on line {$this->filter->line}.");
- 603: }
- 605: return $name . '=' . <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatModifiers('ob_get_clean()', $modifiers);
- 606: }
- 610: /**
- 611: * Converts {block named}...{/block} to functions.
- 612: */
- 613: private function cbNamedBlocks($matches)
- 614: {
- 615: list(, $name, $content) = $matches;
- 616: $func = '_cbb' . substr(md5($this->uniq . $name), 0, 10) . '_' . preg_replace('#[^a-z0-9_]#i', '_', $name);
- 618: . "if (!function_exists(\$_cb->blocks[" . var_export($name, TRUE) . "][] = '$func')) { function $func() { extract(func_get_arg(0))\n?>$content<?php\n}}";
- 619: return '';
- 620: }
- 624: /**
- 625: * {foreach ...}
- 626: */
- 627: private function macroForeach($content)
- 628: {
- 629: return '$iterator = $_cb->its[] = new SmartCachingIterator(' . preg_replace('# +as +#i', ') as ', $content, 1);
- 630: }
- 634: /**
- 635: * {attr ...}
- 636: */
- 637: private function macroAttr($content)
- 638: {
- 639: return preg_replace('#\)\s+#', ')->', $content . ' ');
- 640: }
- 644: /**
- 645: * {contentType ...}
- 646: */
- 647: private function macroContentType($content)
- 648: {
- 669: } else {
- 672: }
- 674: // temporary solution
- 676: }
- 680: /**
- 681: * {dump ...}
- 682: */
- 683: private function macroDump($content)
- 684: {
- 686: }
- 690: /**
- 691: * {widget ...}
- 692: */
- 693: private function macroWidget($content)
- 694: {
- 695: $pair = <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::fetchToken($content); // widget[:method]
- 696: if ($pair === NULL) {
- 697: throw new <a href="../Nette/InvalidStateException.html">InvalidStateException</a>("Missing widget name in {widget} on line {$this->filter->line}.");
- 698: }
- 699: $pair = explode(':', $pair, 2);
- 700: $widget = <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatString($pair[0]);
- 702: $method = preg_match('#^('.<a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::RE_IDENTIFIER.'|)$#', $method) ? "render$method" : "{\"render$method\"}";
- 703: $param = LatteFilter::formatArray($content);
- 707: }
- 711: /**
- 712: * {link ...}
- 713: */
- 714: private function macroLink($content, $modifiers)
- 715: {
- 716: return LatteFilter::formatModifiers('$control->link(' . $this->formatLink($content) .')', $modifiers);
- 717: }
- 721: /**
- 722: * {plink ...}
- 723: */
- 724: private function macroPlink($content, $modifiers)
- 725: {
- 726: return <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatModifiers('$presenter->link(' . $this->formatLink($content) .')', $modifiers);
- 727: }
- 731: /**
- 732: * {ifCurrent ...}
- 733: */
- 734: private function macroIfCurrent($content)
- 735: {
- 736: return $content ? 'try { $presenter->link(' . $this->formatLink($content) . '); } catch (InvalidLinkException $e) {}' : '';
- 737: }
- 741: /**
- 742: * Formats {*link ...} parameters.
- 743: */
- 744: private function formatLink($content)
- 745: {
- 746: return <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatString(<a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::fetchToken($content)) . <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatArray($content, ', '); // destination [,] args
- 747: }
- 751: /**
- 752: * {assign ...}
- 753: */
- 754: private function macroAssign($content, $modifiers)
- 755: {
- 756: if (!$content) {
- 757: throw new <a href="../Nette/InvalidStateException.html">InvalidStateException</a>("Missing arguments in {assign} on line {$this->filter->line}.");
- 758: }
- 759: if (strpos($content, '=>') === FALSE) { // back compatibility
- 760: return '$' . ltrim(<a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::fetchToken($content), '$') . ' = ' . <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatModifiers($content === '' ? 'NULL' : $content, $modifiers);
- 761: }
- 762: return 'extract(' . <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatArray($content) . ')';
- 763: }
- 767: /**
- 768: * {default ...}
- 769: */
- 770: private function macroDefault($content)
- 771: {
- 772: if (!$content) {
- 773: throw new <a href="../Nette/InvalidStateException.html">InvalidStateException</a>("Missing arguments in {default} on line {$this->filter->line}.");
- 774: }
- 775: return 'extract(' . <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatArray($content) . ', EXTR_SKIP)';
- 776: }
- 780: /**
- 781: * Escaping helper.
- 782: */
- 783: private function macroEscape($content)
- 784: {
- 786: }
- 790: /**
- 791: * Just modifiers helper.
- 792: */
- 793: private function macroModifiers($content, $modifiers)
- 794: {
- 795: return <a href="../Nette-Templates/LatteFilter.html">LatteFilter</a>::formatModifiers($content, $modifiers);
- 796: }
- 800: /********************* run-time helpers ****************d*g**/
- 804: /**
- 805: * Calls block.
- 806: * @param array
- 807: * @param string
- 808: * @param array
- 809: * @return void
- 810: */
- 811: public static function callBlock(& $blocks, $name, $params)
- 812: {
- 813: if (empty($blocks[$name])) {
- 814: throw new InvalidStateException("Call to undefined block '$name'.");
- 815: }
- 817: $block($params);
- 818: }
- 822: /**
- 823: * Calls parent block.
- 824: * @param array
- 825: * @param string
- 826: * @param array
- 827: * @return void
- 828: */
- 829: public static function callBlockParent(& $blocks, $name, $params)
- 830: {
- 831: if (empty($blocks[$name]) || ($block = next($blocks[$name])) === FALSE) {
- 833: }
- 834: $block($params);
- 835: }
- 839: /**
- 840: * Includes subtemplate.
- 841: * @param mixed included file name or template
- 842: * @param array parameters
- 843: * @param ITemplate current template
- 844: * @return Template
- 845: */
- 846: public static function includeTemplate($destination, $params, $template)
- 847: {
- 848: if ($destination instanceof ITemplate) {
- 849: $tpl = $destination;
- 851: } elseif ($destination == NULL) { // intentionally ==
- 852: throw new InvalidArgumentException("Template file name was not specified.");
- 854: } else {
- 855: $tpl = clone $template;
- 856: if ($template instanceof IFileTemplate) {
- 857: if (substr($destination, 0, 1) !== '/' && substr($destination, 1, 1) !== ':') {
- 858: $destination = dirname($template->getFile()) . '/' . $destination;
- 859: }
- 860: $tpl->setFile($destination);
- 861: }
- 862: }
- 864: $tpl->setParams($params); // interface?
- 865: return $tpl;
- 866: }
- 870: /**
- 871: * Initializes state holder $_cb in template.
- 872: * @param ITemplate
- 873: * @param bool
- 874: * @param string
- 875: * @return stdClass
- 876: */
- 877: public static function initRuntime($template, $extends, $realFile)
- 878: {
- 879: $cb = (object) NULL;
- 881: // extends support
- 882: if (isset($template->_cb)) {
- 883: $cb->blocks = & $template->_cb->blocks;
- 884: $cb->templates = & $template->_cb->templates;
- 885: }
- 886: $cb->templates[$realFile] = $template;
- 887: $cb->extends = is_bool($extends) ? $extends : (empty($template->_extends) ? FALSE : $template->_extends);
- 888: unset($template->_cb, $template->_extends);
- 890: // cache support
- 891: if (!empty($cb->caches)) {
- 893: }
- 895: return $cb;
- 896: }