Yii Framework 2.0 Widgets

Source: Internet
Author: User

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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.