Widget benefits are aspect reuse and flexible mobility. CWidget is the Widge parent class, and it is Cbasecontroller subclass, CWidget provides some methods similar to Ccontroller, but the render () method renders without layout, and the rendering time is $ This refers to the CWidget object, not the Ccontroller object, which can be obtained by the Getcontroller () method in which the Ccontroller object needs to be overloaded with the Init () and run () methods in its subclasses to customize the different pendants.
Cbasecontroller provides the widget () method and the Beginwidget (), Endwidget () method to load the pendant.
[PHP]View Plaincopy
- Public function Widget ($className,$properties =Array (),$captureOutput =false)
- {
- if ($captureOutput)
- {
- Ob_start ();
- Ob_implicit_flush (FALSE);
- $widget =$this->createwidget ($className,$properties);
- $widget->run ();
- return Ob_get_clean ();
- }
- Else
- {
- $widget =$this->createwidget ($className,$properties);
- $widget->run ();
- return $widget;
- }
- }
- Public function Createwidget ($className,$properties =Array ())
- {
- $widget =yii::app ()->getwidgetfactory ()->createwidget ($this,$className,$properties);
- $widget->init ();
- return $widget;
- }
Above is the direct loading of the pendant method, the third parameter to determine whether to return content or direct output content, the widget is created by Cwidgetfactory.
For example, the following is the code in the view file, which is common to site breadcrumbs
[PHP]View Plaincopy
- $this->breadcrumbs=Array (
- <span style="White-space:pre" > </span>' Users ',
- );
- $this->widget (' zii.widgets.CBreadcrumbs ', array (
- <span style="White-space:pre" > </span>' links ' +$this->breadcrumbs,
- ));
Through the Cbasecontroller widget () method, the Cbreadcrumbs pendant is created, and after initialization, the run () method is executed to render the content.
[PHP]View Plaincopy
- /**
- * Renders the content of the portlet.
- */
- Public function run ()
- {
- if (empty ($this->links))
- return;
- Echo Chtml::opentag ($this->tagname,$this->htmloptions)." \ n ";
- $links =Array ();
- if ($this->homelink===null)
- $links []=chtml::link (Yii::t (' zii ',' Home '), Yii::app ()->homeurl);
- Else if ($this->homelink!==false)
- $links []=$this->homelink;
- foreach ($this->links as $label =$url)
- {
- if (is_string ($label) | | Is_array ($url))
- $links []=chtml::link ($this->encodelabel? Chtml::encode ($label): $label, $url);
- Else
- $links []=' <span> '. ( $this->encodelabel? Chtml::encode ($url): $url).' </span> ';
- }
- Echo implode ($this->separator,$links);
- Echo Chtml::closetag ($this->tagname);
- }
Another multi-level layout example to illustrate the use of Beginwidget (), Endwidget (). In the scaffolding-generated code, the code in the Column1 under layout
[PHP]View Plaincopy
- <?php $this->begincontent ('//layouts/main ');?>
- <div id="Content" >
- <?php echo $content;?>
- </div><!--Content--
- <?php $this->endcontent ();?>
The code in Begincontent ($view =null, $data =array ()) is actually the beginwidget (' Ccontentdecorator ', Array (' view ' = = $view, ' data ' = > $data)) re-package, create the content decoration pendant ccontentdecorator, the main view in,
The difference from the widget () is that the $this->_widgetstack[]= $widget and $widget=array_pop ($this->_widgetstack)), the application stack to manipulate the pendant, The content in the middle of the two methods is captured by the PHP content output buffer function, and all of the above code is used to <div id= "content" ><?php echo $content;?></div> with variables ($content ) is passed to main.php in the same way.
Yii Framework Analysis Note 7: Pendant widgets