Qiang Xue
12 years ago
5 changed files with 249 additions and 174 deletions
@ -0,0 +1,57 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yii\widgets; |
||||||
|
|
||||||
|
use Yii; |
||||||
|
use yii\base\Widget; |
||||||
|
use yii\base\View; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Qiang Xue <qiang.xue@gmail.com> |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
class Clip extends Widget |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var string the ID of this clip. |
||||||
|
*/ |
||||||
|
public $id; |
||||||
|
/** |
||||||
|
* @var View the view object for keeping the clip. If not set, the view registered with the application |
||||||
|
* will be used. |
||||||
|
*/ |
||||||
|
public $view; |
||||||
|
/** |
||||||
|
* @var boolean whether to render the clip content in place. Defaults to false, |
||||||
|
* meaning the captured clip will not be displayed. |
||||||
|
*/ |
||||||
|
public $renderInPlace = false; |
||||||
|
|
||||||
|
/** |
||||||
|
* Starts recording a clip. |
||||||
|
*/ |
||||||
|
public function init() |
||||||
|
{ |
||||||
|
ob_start(); |
||||||
|
ob_implicit_flush(false); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Ends recording a clip. |
||||||
|
* This method stops output buffering and saves the rendering result as a named clip in the controller. |
||||||
|
*/ |
||||||
|
public function run() |
||||||
|
{ |
||||||
|
$clip = ob_get_clean(); |
||||||
|
if ($this->renderClip) { |
||||||
|
echo $clip; |
||||||
|
} |
||||||
|
$view = $this->view !== null ? $this->view : Yii::$app->getView(); |
||||||
|
$view->clips[$this->id] = $clip; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,81 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* CContentDecorator class file. |
||||||
|
* |
||||||
|
* @author Qiang Xue <qiang.xue@gmail.com> |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright 2008-2013 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
/** |
||||||
|
* 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> |
||||||
|
* @package system.web.widgets |
||||||
|
* @since 1.0 |
||||||
|
*/ |
||||||
|
class CContentDecorator extends COutputProcessor |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var mixed the name of the view that will be used to decorate the captured content. |
||||||
|
* If this property is null (default value), 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}. |
||||||
|
*/ |
||||||
|
public $view; |
||||||
|
/** |
||||||
|
* @var array the variables (name=>value) to be extracted and made available in the decorative view. |
||||||
|
*/ |
||||||
|
public $data=array(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Processes the captured output. |
||||||
|
* This method decorates the output with the specified {@link view}. |
||||||
|
* @param string $output the captured output to be processed |
||||||
|
*/ |
||||||
|
public function processOutput($output) |
||||||
|
{ |
||||||
|
$output=$this->decorate($output); |
||||||
|
parent::processOutput($output); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Decorates the content by rendering a view and embedding the content in it. |
||||||
|
* The content being embedded can be accessed in the view using variable <code>$content</code> |
||||||
|
* The decorated content will be displayed directly. |
||||||
|
* @param string $content the content to be decorated |
||||||
|
* @return string the decorated content |
||||||
|
*/ |
||||||
|
protected function decorate($content) |
||||||
|
{ |
||||||
|
$owner=$this->getOwner(); |
||||||
|
if($this->view===null) |
||||||
|
$viewFile=Yii::app()->getController()->getLayoutFile(null); |
||||||
|
else |
||||||
|
$viewFile=$owner->getViewFile($this->view); |
||||||
|
if($viewFile!==false) |
||||||
|
{ |
||||||
|
$data=$this->data; |
||||||
|
$data['content']=$content; |
||||||
|
return $owner->renderFile($viewFile,$data,true); |
||||||
|
} |
||||||
|
else |
||||||
|
return $content; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue