Widgets are reusable units in a view.
Widgets are used in the view, but may need to be passed to his model using a controller, such as when rendering a form. For example, the general time Picker can directly hit the view to add the following code can be:
<? PHP Use yii\jui\datepicker;? ><?= datepicker::widget ([' name ' = ' Date '])?>
If the model is used, this is basically the case:
<? PHP Use yii\jui\datepicker;? ><?= DatePicker::widgets ([ $model, ' attribute ' = ' from_date ', ' language ' = ' ru ', ' clientoptions ' = [ ' dateformat ' = ' yy-mm-dd ', ], ?>
Some widgets can use data content in [[Yii\base\widget::begin ()]] and [[Yii\base\widget::end ()]] calls. For example, ActiveForm widgets are used in this way.
<? PHP Use Yii\widgets\activeform; Use yii\helpers\html; $form = Activeform::begin ([' id ' = ' login-form ']);?> $form->field ($model, ' Username ')?> $form->field ($model, ' password ')->passwordinput ()?> class= "Form-group" > <?= Html::submitbutton (' Login ')?> </div><?php ActiveForm::end();?>
Unlike calling [[Yii\base\widget::widget ()]] to return render results, the [[Yii\base\widget::begin ()]] method is called to return a widget instance that can assemble the widget content.
To create a widget:
You can create a widget by inheriting Yii\base\widget and implementing the Init and run methods.
1. Create a widget that can be output directly like the time selector mentioned above:
namespace app\components; UseYii\base\widget; Useyii\helpers\html;classHellowidgetextendsWidget { Public $message; Public functionInit () {Parent::init (); if(NULL==$this-message) { $this->message = ' Hello world '; } } Public functionrun () {returnHtml::encode ($this-message); }}
The code used in the template is:
<? PHP Use app\components\hellowidget;? ><?= hellowidget::widget ([' message ' = ' This is a hellowidget! '])? >
2. If you want to create a widget that uses begin and end:
amespace app\components; UseYii\base\widget; Useyii\helpers\html;classHellowidgetextendsWidget { Public $message; Public functionInit () {Parent::init (); Ob_start(); } Public functionfield () {returnHtml::encode (' This is the method that the instance calls! ‘); } Public functionrun () {$this->message =Ob_get_clean(); returnHtml::encode ($this-message); }}
The call in the template is this:
<? PHP Use yii\helpers\html; Use App\components\hellowidget; Echo Html::encode ($message$hello = Hellowidget::begin ();? > This time the use of the Begin and end method. $hello->field ();? ><?php hellowidget::end();? >
Sometimes widgets need to render a lot of content, a better way is to put the content into a view file, and then call the [[Yii\base\widget::render ()]] method to render the view file, the widget's view file is stored in the Widgetpath/views directory by default, Widgetpath represents the directory where the widget class files reside. If the sample widget class file above is @app/components, the @app/components/views/hello.php view file is rendered. You can override the [[Yii\base\widget::getviewpath ()]] method to customize the path where the view file is located.
Public function run () { return$this->render (' hello ');}
The last point to mention should be noted:
- It is still necessary to follow the MVC pattern when creating widgets, usually logic code in the Widget class, showing the content in the view.
- The widget should be designed independently, meaning that when a widget is used, it can be dropped directly without the need for additional processing. But when widgets require external resources such as CSS, JavaScript, pictures, and so on, it can be tricky and, fortunately, Yii provides resource packages to solve this problem.
- When a widget contains only the view code, it is similar to the view, in fact, the only difference in this case is that the widget is reusable, and the view is just the normal PHP script used in the app.
Yii Framework 2.0 Widgets