This example describes the use of widgets in Yii. Share to everyone for your reference. The specific analysis is as follows:
The widget definition is simple, if you create a widget called "testwidget.php" under/protected/widget/test/.
Calling in the view needs to be written like this:
Copy Code code as follows:
<?php
$this->beginwidget (' Application.widget.test.testWidget ');
?>
//... Maybe the widget needs to be used here for the main content ...
<?php
$this->endwidget ();
?>
testwidget.php how the file is defined:
Copy Code code as follows:
/**
* Test Widget
*/
Class Testwidget extends CWidget
{
Public Function init ()
{
This method is executed when the $this->beginwidget () is executed in the view
You can do the query data operation here
}
Public Function Run ()
{
This method is executed when the $this->endwidget () is executed in the view
You can do the rendering attempt here, and note that the view mentioned here is the widget view
Note that the widget's view is placed under the views directory with the widget sibling, for example, the following view is placed in the
/protected/widget/test/views/test.php
$this->render (' Test ', Array (
' str ' => ' widget view variable ',
));
}
}
I hope this article will help you with the PHP program design based on the YII framework.