選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

70 行
1.7KB

  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. * Block records all output between [[begin()]] and [[end()]] calls and stores it in [[\yii\base\View::$blocks]].
  11. * for later use.
  12. *
  13. * [[\yii\base\View]] component contains two methods [[\yii\base\View::beginBlock()]] and [[\yii\base\View::endBlock()]].
  14. * The general idea is that you're defining block default in a view or layout:
  15. *
  16. * ```php
  17. * <?php $this->beginBlock('messages', true) ?>
  18. * Nothing.
  19. * <?php $this->endBlock() ?>
  20. * ```
  21. *
  22. * And then overriding default in sub-views:
  23. *
  24. * ```php
  25. * <?php $this->beginBlock('username') ?>
  26. * Umm... hello?
  27. * <?php $this->endBlock() ?>
  28. * ```
  29. *
  30. * Second parameter defines if block content should be outputted which is desired when rendering its content but isn't
  31. * desired when redefining it in subviews.
  32. *
  33. * @author Qiang Xue <qiang.xue@gmail.com>
  34. * @since 2.0
  35. */
  36. class Block extends Widget
  37. {
  38. /**
  39. * @var boolean whether to render the block content in place. Defaults to false,
  40. * meaning the captured block content will not be displayed.
  41. */
  42. public $renderInPlace = false;
  43. /**
  44. * Starts recording a block.
  45. */
  46. public function init()
  47. {
  48. ob_start();
  49. ob_implicit_flush(false);
  50. }
  51. /**
  52. * Ends recording a block.
  53. * This method stops output buffering and saves the rendering result as a named block in the view.
  54. */
  55. public function run()
  56. {
  57. $block = ob_get_clean();
  58. if ($this->renderInPlace) {
  59. echo $block;
  60. }
  61. $this->view->blocks[$this->getId()] = $block;
  62. }
  63. }