1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Templates;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22:
23: class SnippetHelper extends Nette\Object
24: {
25:
26: public static $outputAllowed = TRUE;
27:
28:
29: private $id;
30:
31:
32: private $tag;
33:
34:
35: private $payload;
36:
37:
38: private $level;
39:
40:
41:
42: 43: 44: 45: 46: 47: 48:
49: public static function create(Nette\Application\Control $control, $name = NULL, $tag = 'div')
50: {
51: if (self::$outputAllowed) {
52: $obj = new self;
53: $obj->tag = trim($tag, '<>');
54: if ($obj->tag) echo '<', $obj->tag, ' id="', $control->getSnippetId($name), '">';
55: return $obj;
56:
57: } elseif ($control->isControlInvalid($name)) {
58: $obj = new self;
59: $obj->id = $control->getSnippetId($name);
60: $obj->payload = $control->getPresenter()->getPayload();
61: ob_start();
62: $obj->level = ob_get_level();
63: self::$outputAllowed = TRUE;
64: return $obj;
65:
66: } else {
67: return FALSE;
68: }
69: }
70:
71:
72:
73: 74: 75: 76:
77: public function finish()
78: {
79: if ($this->tag !== NULL) {
80: if ($this->tag) echo "</$this->tag>";
81:
82: } else {
83: if ($this->level !== ob_get_level()) {
84: throw new \InvalidStateException("Snippet '$this->id' cannot be ended here.");
85: }
86: $this->payload->snippets[$this->id] = ob_get_clean();
87: self::$outputAllowed = FALSE;
88: }
89: }
90:
91: }
92: