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
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • Loader
  • SnippetBridge
  • Template
  • TemplateFactory
  • UIMacros
  • UIRuntime

Interfaces

  • ILatteFactory
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  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 Latte;
 11: use Latte\CompileException;
 12: use Latte\MacroNode;
 13: use Latte\PhpWriter;
 14: use Nette;
 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:  * - n:nonce
 25:  */
 26: class UIMacros extends Latte\Macros\MacroSet
 27: {
 28:     /** @var bool|string */
 29:     private $extends;
 30: 
 31: 
 32:     public static function install(Latte\Compiler $compiler)
 33:     {
 34:         $me = new static($compiler);
 35:         $me->addMacro('control', [$me, 'macroControl']);
 36: 
 37:         $me->addMacro('href', null, null, function (MacroNode $node, PhpWriter $writer) use ($me) {
 38:             return ' ?> href="<?php ' . $me->macroLink($node, $writer) . ' ?>"<?php ';
 39:         });
 40:         $me->addMacro('plink', [$me, 'macroLink']);
 41:         $me->addMacro('link', [$me, 'macroLink']);
 42:         $me->addMacro('ifCurrent', [$me, 'macroIfCurrent'], '}'); // deprecated; use n:class="$presenter->linkCurrent ? ..."
 43:         $me->addMacro('extends', [$me, 'macroExtends']);
 44:         $me->addMacro('layout', [$me, 'macroExtends']);
 45:         $me->addMacro('nonce', null, null, 'echo $this->global->uiNonce ? " nonce=\"{$this->global->uiNonce}\"" : "";');
 46:     }
 47: 
 48: 
 49:     /**
 50:      * Initializes before template parsing.
 51:      * @return void
 52:      */
 53:     public function initialize()
 54:     {
 55:         $this->extends = false;
 56:     }
 57: 
 58: 
 59:     /**
 60:      * Finishes template parsing.
 61:      * @return array(prolog, epilog)
 62:      */
 63:     public function finalize()
 64:     {
 65:         return [$this->extends . 'Nette\Bridges\ApplicationLatte\UIRuntime::initialize($this, $this->parentName, $this->blocks);'];
 66:     }
 67: 
 68: 
 69:     /********************* macros ****************d*g**/
 70: 
 71: 
 72:     /**
 73:      * {control name[:method] [params]}
 74:      */
 75:     public function macroControl(MacroNode $node, PhpWriter $writer)
 76:     {
 77:         $words = $node->tokenizer->fetchWords();
 78:         if (!$words) {
 79:             throw new CompileException('Missing control name in {control}');
 80:         }
 81:         $name = $writer->formatWord($words[0]);
 82:         $method = isset($words[1]) ? ucfirst($words[1]) : '';
 83:         $method = Strings::match($method, '#^\w*\z#') ? "render$method" : "{\"render$method\"}";
 84: 
 85:         $tokens = $node->tokenizer;
 86:         $pos = $tokens->position;
 87:         $param = $writer->formatArray();
 88:         $tokens->position = $pos;
 89:         while ($tokens->nextToken()) {
 90:             if ($tokens->isCurrent('=>') && !$tokens->depth) {
 91:                 $wrap = true;
 92:                 break;
 93:             }
 94:         }
 95:         if (empty($wrap)) {
 96:             $param = substr($param, 1, -1); // removes array() or []
 97:         }
 98:         return "/* line $node->startLine */ "
 99:             . ($name[0] === '$' ? "if (is_object($name)) \$_tmp = $name; else " : '')
100:             . '$_tmp = $this->global->uiControl->getComponent(' . $name . '); '
101:             . 'if ($_tmp instanceof Nette\Application\UI\IRenderable) $_tmp->redrawControl(null, false); '
102:             . ($node->modifiers === ''
103:                 ? "\$_tmp->$method($param);"
104:                 : $writer->write("ob_start(function () {}); \$_tmp->$method($param); echo %modify(ob_get_clean());")
105:             );
106:     }
107: 
108: 
109:     /**
110:      * {link destination [,] [params]}
111:      * {plink destination [,] [params]}
112:      * n:href="destination [,] [params]"
113:      */
114:     public function macroLink(MacroNode $node, PhpWriter $writer)
115:     {
116:         $node->modifiers = preg_replace('#\|safeurl\s*(?=\||\z)#i', '', $node->modifiers);
117:         return $writer->using($node)
118:             ->write('echo %escape(%modify('
119:                 . ($node->name === 'plink' ? '$this->global->uiPresenter' : '$this->global->uiControl')
120:                 . '->link(%node.word, %node.array?)))'
121:             );
122:     }
123: 
124: 
125:     /**
126:      * {ifCurrent destination [,] [params]}
127:      */
128:     public function macroIfCurrent(MacroNode $node, PhpWriter $writer)
129:     {
130:         if ($node->modifiers) {
131:             throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
132:         }
133:         return $writer->write($node->args
134:             ? 'if ($this->global->uiPresenter->isLinkCurrent(%node.word, %node.array?)) {'
135:             : 'if ($this->global->uiPresenter->getLastCreatedRequestFlag("current")) {'
136:         );
137:     }
138: 
139: 
140:     /**
141:      * {extends auto}
142:      */
143:     public function macroExtends(MacroNode $node, PhpWriter $writer)
144:     {
145:         if ($node->modifiers || $node->parentNode || $node->args !== 'auto') {
146:             return $this->extends = false;
147:         }
148:         $this->extends = $writer->write('$this->parentName = $this->global->uiPresenter->findLayoutTemplateFile();');
149:     }
150: 
151: 
152:     /** @deprecated */
153:     public static function renderSnippets(Nette\Application\UI\Control $control, \stdClass $local, array $params)
154:     {
155:         trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
156:         UIRuntime::renderSnippets($control, $local, $params);
157:     }
158: }
159: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0