Yii Framework Analysis Note 7: Pendant widgets

Source: Internet
Author: User

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
  1. Public function Widget ($className,$properties =Array (),$captureOutput =false)
  2. {
  3. if ($captureOutput)
  4. {
  5. Ob_start ();
  6. Ob_implicit_flush (FALSE);
  7. $widget =$this->createwidget ($className,$properties);
  8. $widget->run ();
  9. return Ob_get_clean ();
  10. }
  11. Else
  12. {
  13. $widget =$this->createwidget ($className,$properties);
  14. $widget->run ();
  15. return $widget;
  16. }
  17. }
  18. Public function Createwidget ($className,$properties =Array ())
  19. {
  20. $widget =yii::app ()->getwidgetfactory ()->createwidget ($this,$className,$properties);
  21. $widget->init ();
  22. return $widget;
  23. }

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
    1. $this->breadcrumbs=Array (
    2. <span style="White-space:pre" > </span>' Users ',
    3. );
    4. $this->widget (' zii.widgets.CBreadcrumbs ', array (
    5. <span style="White-space:pre" > </span>' links ' +$this->breadcrumbs,
    6. ));

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
  1. /**
  2. * Renders the content of the portlet.
  3. */
  4. Public function run ()
  5. {
  6. if (empty ($this->links))
  7. return;
  8. Echo Chtml::opentag ($this->tagname,$this->htmloptions)."  \ n ";
  9. $links =Array ();
  10. if ($this->homelink===null)
  11. $links []=chtml::link (Yii::t (' zii ',' Home '), Yii::app ()->homeurl);
  12. Else if ($this->homelink!==false)
  13. $links []=$this->homelink;
  14. foreach ($this->links as $label =$url)
  15. {
  16. if (is_string ($label) | | Is_array ($url))
  17. $links []=chtml::link ($this->encodelabel?  Chtml::encode ($label): $label, $url);
  18. Else
  19. $links []=' <span> '. ( $this->encodelabel? Chtml::encode ($url): $url).'  </span> ';
  20. }
  21. Echo implode ($this->separator,$links);
  22. Echo Chtml::closetag ($this->tagname);
  23. }


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
    1. <?php $this->begincontent ('//layouts/main ');?>
    2. <div id="Content" >
    3. <?php echo $content;?>
    4. </div><!--Content--
    5. <?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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.