Namespaces

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • BaseTemplate
  • CachingHelper
  • CurlyBracketsMacros
  • LatteFilter
  • LatteMacros
  • SnippetHelper
  • Template
  • TemplateCacheStorage
  • TemplateFilters
  • TemplateHelpers

Interfaces

  • IFileTemplate
  • ITemplate
  • Overview
  • Namespace
  • Class
  • Tree
  • Other releases
 1: <?php
 2: 
 3: /**
 4:  * This file is part of the Nette Framework (https://nette.org)
 5:  *
 6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
 7:  *
 8:  * For the full copyright and license information, please view
 9:  * the file license.txt that was distributed with this source code.
10:  */
11: 
12: namespace Nette\Templates;
13: 
14: use Nette;
15: 
16: 
17: 
18: /**
19:  * Control snippet template helper.
20:  *
21:  * @author     David Grudl
22:  */
23: class SnippetHelper extends Nette\Object
24: {
25:     /** @var bool */
26:     public static $outputAllowed = TRUE;
27: 
28:     /** @var string */
29:     private $id;
30: 
31:     /** @var string */
32:     private $tag;
33: 
34:     /** @var ArrayObject */
35:     private $payload;
36: 
37:     /** @var int */
38:     private $level;
39: 
40: 
41: 
42:     /**
43:      * Starts conditional snippet rendering. Returns SnippetHelper object if snippet was started.
44:      * @param  Control control
45:      * @param  string  snippet name
46:      * @param  string  start element
47:      * @return SnippetHelper
48:      */
49:     public static function create(Nette\Application\Control $control, $name = NULL, $tag = 'div')
50:     {
51:         if (self::$outputAllowed) { // rendering flow or non-AJAX request
52:             $obj = new self;
53:             $obj->tag = trim($tag, '<>');
54:             if ($obj->tag) echo '<', $obj->tag, ' id="', $control->getSnippetId($name), '">';
55:             return $obj; // or string?
56: 
57:         } elseif ($control->isControlInvalid($name)) { // start snippet buffering
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:      * Finishes and saves the snippet.
75:      * @return void
76:      */
77:     public function finish()
78:     {
79:         if ($this->tag !== NULL) { // rendering flow or non-AJAX request
80:             if ($this->tag) echo "</$this->tag>";
81: 
82:         } else {  // finish snippet buffering
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: 
Nette Framework 0.9.7 API documentation generated by ApiGen 2.3.0