Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • none

Classes

  • CacheMacro
  • CoreMacros
  • FormMacros
  • MacroSet
  • UIMacros
  • 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\Latte\Macros;
  9: 
 10: use Nette;
 11: use Nette\Latte;
 12: use Nette\Latte\MacroNode;
 13: 
 14: 
 15: /**
 16:  * Base IMacro implementation. Allows add multiple macros.
 17:  *
 18:  * @author     David Grudl
 19:  */
 20: class MacroSet extends Nette\Object implements Latte\IMacro
 21: {
 22:     /** @var Latte\Compiler */
 23:     private $compiler;
 24: 
 25:     /** @var array */
 26:     private $macros;
 27: 
 28: 
 29:     public function __construct(Latte\Compiler $compiler)
 30:     {
 31:         $this->compiler = $compiler;
 32:     }
 33: 
 34: 
 35:     public function addMacro($name, $begin, $end = NULL, $attr = NULL)
 36:     {
 37:         foreach (array($begin, $end, $attr) as $arg) {
 38:             if ($arg && !is_string($arg)) {
 39:                 Nette\Utils\Callback::check($arg);
 40:             }
 41:         }
 42: 
 43:         $this->macros[$name] = array($begin, $end, $attr);
 44:         $this->compiler->addMacro($name, $this);
 45:         return $this;
 46:     }
 47: 
 48: 
 49:     public static function install(Latte\Compiler $compiler)
 50:     {
 51:         return new static($compiler);
 52:     }
 53: 
 54: 
 55:     /**
 56:      * Initializes before template parsing.
 57:      * @return void
 58:      */
 59:     public function initialize()
 60:     {
 61:     }
 62: 
 63: 
 64:     /**
 65:      * Finishes template parsing.
 66:      * @return array(prolog, epilog)
 67:      */
 68:     public function finalize()
 69:     {
 70:     }
 71: 
 72: 
 73:     /**
 74:      * New node is found.
 75:      * @return bool
 76:      */
 77:     public function nodeOpened(MacroNode $node)
 78:     {
 79:         list($begin, $end, $attr) = $this->macros[$node->name];
 80:         $node->isEmpty = !$end;
 81: 
 82:         if ($attr && $node->prefix === $node::PREFIX_NONE) {
 83:             $node->isEmpty = TRUE;
 84:             $this->compiler->setContext(Latte\Compiler::CONTEXT_DOUBLE_QUOTED_ATTR);
 85:             $res = $this->compile($node, $attr);
 86:             if ($res === FALSE) {
 87:                 return FALSE;
 88:             } elseif (!$node->attrCode) {
 89:                 $node->attrCode = "<?php $res ?>";
 90:             }
 91:             $this->compiler->setContext(NULL);
 92: 
 93:         } elseif ($begin) {
 94:             $res = $this->compile($node, $begin);
 95:             if ($res === FALSE) {
 96:                 return FALSE;
 97:             } elseif (!$node->openingCode) {
 98:                 $node->openingCode = "<?php $res ?>";
 99:             }
100: 
101:         } elseif (!$end) {
102:             return FALSE;
103:         }
104:     }
105: 
106: 
107:     /**
108:      * Node is closed.
109:      * @return void
110:      */
111:     public function nodeClosed(MacroNode $node)
112:     {
113:         if (isset($this->macros[$node->name][1])) {
114:             $res = $this->compile($node, $this->macros[$node->name][1]);
115:             if (!$node->closingCode) {
116:                 $node->closingCode = "<?php $res ?>";
117:             }
118:         }
119:     }
120: 
121: 
122:     /**
123:      * Generates code.
124:      * @return string
125:      */
126:     private function compile(MacroNode $node, $def)
127:     {
128:         $node->tokenizer->reset();
129:         $writer = Latte\PhpWriter::using($node, $this->compiler);
130:         if (is_string($def)) {
131:             return $writer->write($def);
132:         } else {
133:             return Nette\Utils\Callback::invoke($def, $node, $writer);
134:         }
135:     }
136: 
137: 
138:     /**
139:      * @return Latte\Compiler
140:      */
141:     public function getCompiler()
142:     {
143:         return $this->compiler;
144:     }
145: 
146: }
147: 
Nette 2.1 API documentation generated by ApiGen 2.8.0