CContentDecorator.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * CContentDecorator class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CContentDecorator decorates the content it encloses with the specified view.
  12. *
  13. * CContentDecorator is mostly used to implement nested layouts, i.e., a layout
  14. * is embedded within another layout. {@link CBaseController} defines a pair of
  15. * convenient methods to use CContentDecorator:
  16. * <pre>
  17. * $this->beginContent('path/to/view');
  18. * // ... content to be decorated
  19. * $this->endContent();
  20. * </pre>
  21. *
  22. * The property {@link view} specifies the name of the view that is used to
  23. * decorate the content. In the view, the content being decorated may be
  24. * accessed with variable <code>$content</code>.
  25. *
  26. * @author Qiang Xue <qiang.xue@gmail.com>
  27. * @package system.web.widgets
  28. * @since 1.0
  29. */
  30. class CContentDecorator extends COutputProcessor
  31. {
  32. /**
  33. * @var mixed the name of the view that will be used to decorate the captured content.
  34. * If this property is null (default value), the default layout will be used as
  35. * the decorative view. Note that if the current controller does not belong to
  36. * any module, the default layout refers to the application's {@link CWebApplication::layout default layout};
  37. * If the controller belongs to a module, the default layout refers to the module's
  38. * {@link CWebModule::layout default layout}.
  39. */
  40. public $view;
  41. /**
  42. * @var array the variables (name=>value) to be extracted and made available in the decorative view.
  43. */
  44. public $data=array();
  45. /**
  46. * Processes the captured output.
  47. * This method decorates the output with the specified {@link view}.
  48. * @param string $output the captured output to be processed
  49. */
  50. public function processOutput($output)
  51. {
  52. $output=$this->decorate($output);
  53. parent::processOutput($output);
  54. }
  55. /**
  56. * Decorates the content by rendering a view and embedding the content in it.
  57. * The content being embedded can be accessed in the view using variable <code>$content</code>
  58. * The decorated content will be displayed directly.
  59. * @param string $content the content to be decorated
  60. * @return string the decorated content
  61. */
  62. protected function decorate($content)
  63. {
  64. $owner=$this->getOwner();
  65. if($this->view===null)
  66. $viewFile=Yii::app()->getController()->getLayoutFile(null);
  67. else
  68. $viewFile=$owner->getViewFile($this->view);
  69. if($viewFile!==false)
  70. {
  71. $data=$this->data;
  72. $data['content']=$content;
  73. return $owner->renderFile($viewFile,$data,true);
  74. }
  75. else
  76. return $content;
  77. }
  78. }