Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • Loader
  • Template
  • TemplateFactory
  • UIMacros

Interfaces

  • ILatteFactory
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Bridges\ApplicationLatte;
  9: 
 10: use Nette;
 11: use Latte;
 12: use Latte\MacroNode;
 13: use Latte\PhpWriter;
 14: use Latte\CompileException;
 15: use Nette\Utils\Strings;
 16: 
 17: 
 18: /**
 19:  * Macros for Nette\Application\UI.
 20:  *
 21:  * - {link destination ...} control link
 22:  * - {plink destination ...} presenter link
 23:  * - {snippet ?} ... {/snippet ?} control snippet
 24:  */
 25: class UIMacros extends Latte\Macros\MacroSet
 26: {
 27: 
 28:     public static function install(Latte\Compiler $compiler)
 29:     {
 30:         $me = new static($compiler);
 31:         $me->addMacro('control', array($me, 'macroControl'));
 32: 
 33:         $me->addMacro('href', NULL, NULL, function (MacroNode $node, PhpWriter $writer) use ($me) {
 34:             return ' ?> href="<?php ' . $me->macroLink($node, $writer) . ' ?>"<?php ';
 35:         });
 36:         $me->addMacro('plink', array($me, 'macroLink'));
 37:         $me->addMacro('link', array($me, 'macroLink'));
 38:         $me->addMacro('ifCurrent', array($me, 'macroIfCurrent'), '}'); // deprecated; use n:class="$presenter->linkCurrent ? ..."
 39:     }
 40: 
 41: 
 42:     /**
 43:      * Finishes template parsing.
 44:      * @return array(prolog, epilog)
 45:      */
 46:     public function finalize()
 47:     {
 48:         $prolog = '
 49: // snippets support
 50: if (empty($_l->extends) && !empty($_control->snippetMode)) {
 51:     return Nette\Bridges\ApplicationLatte\UIRuntime::renderSnippets($_control, $_b, get_defined_vars());
 52: }';
 53:         return array($prolog, '');
 54:     }
 55: 
 56: 
 57:     /********************* macros ****************d*g**/
 58: 
 59: 
 60:     /**
 61:      * {control name[:method] [params]}
 62:      */
 63:     public function macroControl(MacroNode $node, PhpWriter $writer)
 64:     {
 65:         $words = $node->tokenizer->fetchWords();
 66:         if (!$words) {
 67:             throw new CompileException('Missing control name in {control}');
 68:         }
 69:         $name = $writer->formatWord($words[0]);
 70:         $method = isset($words[1]) ? ucfirst($words[1]) : '';
 71:         $method = Strings::match($method, '#^\w*\z#') ? "render$method" : "{\"render$method\"}";
 72:         $param = $writer->formatArray();
 73:         if (!Strings::contains($node->args, '=>')) {
 74:             $param = substr($param, $param[0] === '[' ? 1 : 6, -1); // removes array() or []
 75:         }
 76:         return ($name[0] === '$' ? "if (is_object($name)) \$_l->tmp = $name; else " : '')
 77:             . '$_l->tmp = $_control->getComponent(' . $name . '); '
 78:             . 'if ($_l->tmp instanceof Nette\Application\UI\IRenderable) $_l->tmp->redrawControl(NULL, FALSE); '
 79:             . ($node->modifiers === '' ? "\$_l->tmp->$method($param)" : $writer->write("ob_start(function () {}); \$_l->tmp->$method($param); echo %modify(ob_get_clean())"));
 80:     }
 81: 
 82: 
 83:     /**
 84:      * {link destination [,] [params]}
 85:      * {plink destination [,] [params]}
 86:      * n:href="destination [,] [params]"
 87:      */
 88:     public function macroLink(MacroNode $node, PhpWriter $writer)
 89:     {
 90:         $node->modifiers = preg_replace('#\|safeurl\s*(?=\||\z)#i', '', $node->modifiers);
 91:         return $writer->using($node, $this->getCompiler())
 92:             ->write('echo %escape(%modify(' . ($node->name === 'plink' ? '$_presenter' : '$_control') . '->link(%node.word, %node.array?)))');
 93:     }
 94: 
 95: 
 96:     /**
 97:      * {ifCurrent destination [,] [params]}
 98:      */
 99:     public function macroIfCurrent(MacroNode $node, PhpWriter $writer)
100:     {
101:         if ($node->modifiers) {
102:             trigger_error("Modifiers are not allowed in {{$node->name}}", E_USER_WARNING);
103:         }
104:         return $writer->write($node->args
105:             ? 'if ($_presenter->isLinkCurrent(%node.word, %node.array?)) {'
106:             : 'if ($_presenter->getLastCreatedRequestFlag("current")) {'
107:         );
108:     }
109: 
110: 
111:     /** @deprecated */
112:     public static function renderSnippets(Nette\Application\UI\Control $control, \stdClass $local, array $params)
113:     {
114:         UIRuntime::renderSnippets($control, $local, $params);
115:     }
116: 
117: }
118: 
Nette 2.3-20161221 API API documentation generated by ApiGen 2.8.0