Source for file SnippetHelper.php

Documentation is available at SnippetHelper.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Templates
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../../Object.php';
  24. 24:  
  25. 25:  
  26. 26:  
  27. 27: /**
  28. 28:  * Control snippet template helper.
  29. 29:  *
  30. 30:  * @author     David Grudl
  31. 31:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  32. 32:  * @package    Nette\Templates
  33. 33:  */
  34. 34: class SnippetHelper extends Object
  35. 35: {
  36. 36:     /** @var bool */
  37. 37:     public static $outputAllowed TRUE;
  38. 38:  
  39. 39:     /** @var string */
  40. 40:     private $id;
  41. 41:  
  42. 42:     /** @var string */
  43. 43:     private $tag;
  44. 44:  
  45. 45:     /** @var ArrayObject */
  46. 46:     private $payload;
  47. 47:  
  48. 48:     /** @var int */
  49. 49:     private $level;
  50. 50:  
  51. 51:  
  52. 52:  
  53. 53:     /**
  54. 54:      * Starts conditional snippet rendering. Returns SnippetHelper object if snippet was started.
  55. 55:      * @param  Control control
  56. 56:      * @param  string  snippet name
  57. 57:      * @param  string  start element
  58. 58:      * @return SnippetHelper 
  59. 59:      */
  60. 60:     public static function create(Control $control$name NULL$tag 'div')
  61. 61:     {
  62. 62:         if (self::$outputAllowed// rendering flow or non-AJAX request
  63. 63:             $obj new self;
  64. 64:             $obj->tag trim($tag'<>');
  65. 65:             if ($obj->tagecho '<'$obj->tag' id="'$control->getSnippetId($name)'">';
  66. 66:             return $obj// or string?
  67. 67:  
  68. 68:         elseif ($control->isControlInvalid($name)) // start snippet buffering
  69. 69:             $obj new self;
  70. 70:             $obj->id $control->getSnippetId($name);
  71. 71:             $obj->payload $control->getPresenter()->getPayload();
  72. 72:             ob_start();
  73. 73:             $obj->level ob_get_level();
  74. 74:             self::$outputAllowed TRUE;
  75. 75:             return $obj;
  76. 76:  
  77. 77:         else {
  78. 78:             return FALSE;
  79. 79:         }
  80. 80:     }
  81. 81:  
  82. 82:  
  83. 83:  
  84. 84:     /**
  85. 85:      * Finishes and saves the snippet.
  86. 86:      * @return void 
  87. 87:      */
  88. 88:     public function finish()
  89. 89:     {
  90. 90:         if ($this->tag !== NULL// rendering flow or non-AJAX request
  91. 91:             if ($this->tagecho "</$this->tag>";
  92. 92:  
  93. 93:         else {  // finish snippet buffering
  94. 94:             if ($this->level !== ob_get_level()) {
  95. 95:                 throw new InvalidStateException("Snippet '$this->id' cannot be ended here.");
  96. 96:             }
  97. 97:             if (empty($this->payload->snippets)) // PHP 5.2.0 bug workaround
  98. 98:                 $this->payload->snippets new ArrayObject;
  99. 99:             }
  100. 100:             $this->payload->snippets[$this->idob_get_clean();
  101. 101:             self::$outputAllowed FALSE;
  102. 102:         }
  103. 103:     }
  104. 104: