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