Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • 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 (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Latte\Macros;
  9: 
 10: use Nette,
 11:     Nette\Latte,
 12:     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:         $this->macros[$name] = array($begin, $end, $attr);
 38:         $this->compiler->addMacro($name, $this);
 39:         return $this;
 40:     }
 41: 
 42: 
 43:     public static function install(Latte\Compiler $compiler)
 44:     {
 45:         return new static($compiler);
 46:     }
 47: 
 48: 
 49:     /**
 50:      * Initializes before template parsing.
 51:      * @return void
 52:      */
 53:     public function initialize()
 54:     {
 55:     }
 56: 
 57: 
 58:     /**
 59:      * Finishes template parsing.
 60:      * @return array(prolog, epilog)
 61:      */
 62:     public function finalize()
 63:     {
 64:     }
 65: 
 66: 
 67:     /**
 68:      * New node is found.
 69:      * @return bool
 70:      */
 71:     public function nodeOpened(MacroNode $node)
 72:     {
 73:         if ($this->macros[$node->name][2] && $node->htmlNode) {
 74:             $node->isEmpty = TRUE;
 75:             $this->compiler->setContext(Latte\Compiler::CONTEXT_DOUBLE_QUOTED);
 76:             $res = $this->compile($node, $this->macros[$node->name][2]);
 77:             $this->compiler->setContext(NULL);
 78:             if (!$node->attrCode) {
 79:                 $node->attrCode = "<?php $res ?>";
 80:             }
 81:         } else {
 82:             $node->isEmpty = !isset($this->macros[$node->name][1]);
 83:             $res = $this->compile($node, $this->macros[$node->name][0]);
 84:             if (!$node->openingCode) {
 85:                 $node->openingCode = "<?php $res ?>";
 86:             }
 87:         }
 88:         return $res !== FALSE;
 89:     }
 90: 
 91: 
 92:     /**
 93:      * Node is closed.
 94:      * @return void
 95:      */
 96:     public function nodeClosed(MacroNode $node)
 97:     {
 98:         $res = $this->compile($node, $this->macros[$node->name][1]);
 99:         if (!$node->closingCode) {
100:             $node->closingCode = "<?php $res ?>";
101:         }
102:     }
103: 
104: 
105:     /**
106:      * Generates code.
107:      * @return string
108:      */
109:     private function compile(MacroNode $node, $def)
110:     {
111:         $node->tokenizer->reset();
112:         $writer = Latte\PhpWriter::using($node, $this->compiler);
113:         if (is_string($def)) {
114:             return $writer->write($def);
115:         } else {
116:             return Nette\Callback::create($def)->invoke($node, $writer);
117:         }
118:     }
119: 
120: 
121:     /**
122:      * @return Latte\Compiler
123:      */
124:     public function getCompiler()
125:     {
126:         return $this->compiler;
127:     }
128: 
129: }
130: 
Nette 2.0 API documentation generated by ApiGen 2.8.0