Block.php 948 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\widgets;
  8. use yii\base\Widget;
  9. /**
  10. * @author Qiang Xue <[email protected]>
  11. * @since 2.0
  12. */
  13. class Block extends Widget
  14. {
  15. /**
  16. * @var string the ID of this block.
  17. */
  18. public $id;
  19. /**
  20. * @var boolean whether to render the block content in place. Defaults to false,
  21. * meaning the captured block content will not be displayed.
  22. */
  23. public $renderInPlace = false;
  24. /**
  25. * Starts recording a block.
  26. */
  27. public function init()
  28. {
  29. ob_start();
  30. ob_implicit_flush(false);
  31. }
  32. /**
  33. * Ends recording a block.
  34. * This method stops output buffering and saves the rendering result as a named block in the controller.
  35. */
  36. public function run()
  37. {
  38. $block = ob_get_clean();
  39. if ($this->renderInPlace) {
  40. echo $block;
  41. }
  42. $this->view->blocks[$this->id] = $block;
  43. }
  44. }