4
* This file is part of the Nette Framework (http://nette.org)
6
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
8
* For the full copyright and license information, please view
9
* the file license.txt that was distributed with this source code.
12
namespace Nette\Latte\Macros;
16
Nette\Latte\MacroNode;
20
* Base IMacro implementation. Allows add multiple macros.
24
class MacroSet extends Nette\Object implements Latte\IMacro
26
/** @var Latte\Compiler */
33
public function __construct(Latte\Compiler $compiler)
35
$this->compiler = $compiler;
39
public function addMacro($name, $begin, $end = NULL, $attr = NULL)
41
$this->macros[$name] = array($begin, $end, $attr);
42
$this->compiler->addMacro($name, $this);
47
public static function install(Latte\Compiler $compiler)
49
return new static($compiler);
54
* Initializes before template parsing.
57
public function initialize()
63
* Finishes template parsing.
64
* @return array(prolog, epilog)
66
public function finalize()
75
public function nodeOpened(MacroNode $node)
77
if ($this->macros[$node->name][2] && $node->prefix) {
78
$node->isEmpty = TRUE;
79
$this->compiler->setContext(Latte\Compiler::CONTEXT_DOUBLE_QUOTED_ATTR);
80
$res = $this->compile($node, $this->macros[$node->name][2]);
81
$this->compiler->setContext(NULL);
82
if (!$node->attrCode) {
83
$node->attrCode = "<?php $res ?>";
86
$node->isEmpty = !isset($this->macros[$node->name][1]);
87
$res = $this->compile($node, $this->macros[$node->name][0]);
88
if (!$node->openingCode) {
89
$node->openingCode = "<?php $res ?>";
92
return $res !== FALSE;
100
public function nodeClosed(MacroNode $node)
102
$res = $this->compile($node, $this->macros[$node->name][1]);
103
if (!$node->closingCode) {
104
$node->closingCode = "<?php $res ?>";
113
private function compile(MacroNode $node, $def)
115
$node->tokenizer->reset();
116
$writer = Latte\PhpWriter::using($node, $this->compiler);
117
if (is_string($def)) {
118
return $writer->write($def);
120
return Nette\Utils\Callback::invoke($def, $node, $writer);
126
* @return Latte\Compiler
128
public function getCompiler()
130
return $this->compiler;