Browse Source

Finished ContentDecorator.

tags/2.0.0-alpha
Qiang Xue 12 years ago
parent
commit
5a6c5ccb4a
  1. 59
      framework/base/View.php
  2. 88
      framework/widgets/ContentDecorator.php

59
framework/base/View.php

@ -119,7 +119,9 @@ class View extends Component
} }
$oldContext = $this->context; $oldContext = $this->context;
$this->context = $context; if ($context !== null) {
$this->context = $context;
}
if ($this->renderer !== null) { if ($this->renderer !== null) {
$output = $this->renderer->render($this, $viewFile, $params); $output = $this->renderer->render($this, $viewFile, $params);
@ -276,6 +278,7 @@ class View extends Component
* @param boolean $renderInPlace whether to render the clip content in place. * @param boolean $renderInPlace whether to render the clip content in place.
* Defaults to false, meaning the captured clip will not be displayed. * Defaults to false, meaning the captured clip will not be displayed.
* @return \yii\widgets\Clip the Clip widget instance * @return \yii\widgets\Clip the Clip widget instance
* @see \yii\widgets\Clip
*/ */
public function beginClip($id, $renderInPlace = false) public function beginClip($id, $renderInPlace = false)
{ {
@ -294,6 +297,31 @@ class View extends Component
$this->endWidget(); $this->endWidget();
} }
/**
* Begins the rendering of content that is to be decorated by the specified view.
* @param string $view the name of the view that will be used to decorate the content enclosed by this widget.
* Please refer to [[View::findViewFile()]] on how to set this property.
* @param array $params the variables (name=>value) to be extracted and made available in the decorative view.
* @return \yii\widgets\ContentDecorator the ContentDecorator widget instance
* @see \yii\widgets\ContentDecorator
*/
public function beginContent($view, $params = array())
{
return $this->beginWidget('yii\widgets\ContentDecorator', array(
'view' => $this,
'viewName' => $view,
'params' => $params,
));
}
/**
* Ends the rendering of content.
*/
public function endContent()
{
$this->endWidget();
}
// //
// /** // /**
// * Begins fragment caching. // * Begins fragment caching.
@ -336,33 +364,4 @@ class View extends Component
// $this->endWidget(); // $this->endWidget();
// } // }
// //
/**
* Begins the rendering of content that is to be decorated by the specified view.
* @param mixed $view the name of the view that will be used to decorate the content. The actual view script
* is resolved via {@link getViewFile}. If this parameter is null (default),
* the default layout will be used as the decorative view.
* Note that if the current controller does not belong to
* any module, the default layout refers to the application's {@link CWebApplication::layout default layout};
* If the controller belongs to a module, the default layout refers to the module's
* {@link CWebModule::layout default layout}.
* @param array $params the variables (name=>value) to be extracted and made available in the decorative view.
* @see endContent
* @see yii\widgets\ContentDecorator
*/
public function beginContent($view, $params = array())
{
$this->beginWidget('yii\widgets\ContentDecorator', array(
'view' => $view,
'params' => $params,
));
}
/**
* Ends the rendering of content.
* @see beginContent
*/
public function endContent()
{
$this->endWidget();
}
} }

88
framework/widgets/ContentDecorator.php

@ -1,81 +1,59 @@
<?php <?php
/** /**
* CContentDecorator class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright 2008-2013 Yii Software LLC * @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\widgets;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\base\View;
/** /**
* CContentDecorator decorates the content it encloses with the specified view.
*
* CContentDecorator is mostly used to implement nested layouts, i.e., a layout
* is embedded within another layout. {@link CBaseController} defines a pair of
* convenient methods to use CContentDecorator:
* <pre>
* $this->beginContent('path/to/view');
* // ... content to be decorated
* $this->endContent();
* </pre>
*
* The property {@link view} specifies the name of the view that is used to
* decorate the content. In the view, the content being decorated may be
* accessed with variable <code>$content</code>.
*
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @package system.web.widgets * @since 2.0
* @since 1.0
*/ */
class CContentDecorator extends COutputProcessor class ContentDecorator extends Widget
{ {
/** /**
* @var mixed the name of the view that will be used to decorate the captured content. * @var View the view object for rendering [[viewName]]. If not set, the view registered with the application
* If this property is null (default value), the default layout will be used as * will be used.
* the decorative view. Note that if the current controller does not belong to
* any module, the default layout refers to the application's {@link CWebApplication::layout default layout};
* If the controller belongs to a module, the default layout refers to the module's
* {@link CWebModule::layout default layout}.
*/ */
public $view; public $view;
/** /**
* @var array the variables (name=>value) to be extracted and made available in the decorative view. * @var string the name of the view that will be used to decorate the content enclosed by this widget.
* Please refer to [[View::findViewFile()]] on how to set this property.
*/ */
public $data=array(); public $viewName;
/**
* @var array the parameters (name=>value) to be extracted and made available in the decorative view.
*/
public $params = array();
/** /**
* Processes the captured output. * Starts recording a clip.
* This method decorates the output with the specified {@link view}.
* @param string $output the captured output to be processed
*/ */
public function processOutput($output) public function init()
{ {
$output=$this->decorate($output); if ($this->viewName === null) {
parent::processOutput($output); throw new InvalidConfigException('ContentDecorator::viewName must be set.');
}
ob_start();
ob_implicit_flush(false);
} }
/** /**
* Decorates the content by rendering a view and embedding the content in it. * Ends recording a clip.
* The content being embedded can be accessed in the view using variable <code>$content</code> * This method stops output buffering and saves the rendering result as a named clip in the controller.
* The decorated content will be displayed directly.
* @param string $content the content to be decorated
* @return string the decorated content
*/ */
protected function decorate($content) public function run()
{ {
$owner=$this->getOwner(); $params = $this->params;
if($this->view===null) $params['content'] = ob_get_clean();
$viewFile=Yii::app()->getController()->getLayoutFile(null); $view = $this->view !== null ? $this->view : Yii::$app->getView();
else echo $view->render($this->viewName, $params);
$viewFile=$owner->getViewFile($this->view);
if($viewFile!==false)
{
$data=$this->data;
$data['content']=$content;
return $owner->renderFile($viewFile,$data,true);
}
else
return $content;
} }
} }

Loading…
Cancel
Save