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