YII2 Widget (Widgets)

Source: Internet
Author: User

Widgets are basically used in views, and the Yii\base\widget::widget () method can be called in the view to use widgets. The method initializes the widget with a configuration array and returns the result after the widget is rendered. For example, the following code inserts a date selector widget, which is configured to use the Russian language, and the input box content is $model the from_date property value.

 <?phpuse yii\jui\datepicker;<?= datepicker::widget ([ ' model ' = $model,  ' attribute ' =  ' from_date ',  ' language ' = Span class= "hljs-string", "Ru",  ' clientoptions ' = [ ' Yy-mm-dd ',],]) ?>    

Some widgets can use data content in Yii\base\widget::begin () and Yii\base\widget::end () calls. For example, the following code uses the Yii\widgets\activeform widget to generate a login form, and the widget will begin() end() generate the start and end tags separately at the 0 execution <form> , and any code in the middle will be rendered.

<?phpUseYii\Widgets\ActiveForm;UseYii\Helpers\Html;?><?php $form = Activeform::begin ([ ' id ' = =  Login-form '); ?> <?= $form->field ($model,  Username ') ?> <?= $form->field ($model,  password ')->passwordinput () ?> <div class= "form-group" > <?= html::submitbutton (' login ')?> </DIV><? php activeform::end ();?;   

Note and call Yii\base\widget::widget () to return different rendering results, call the Yii\base\widget::begin () method to return a widget instance that can assemble the widget content.

Configuring global Defaults

Global defaults for a widget type could is configured via DI container:

\Yii::$container->set(‘yii\widgets\LinkPager‘, [‘maxButtonCount‘ => 5]);

See "Practical Usage" sections in Dependency Injection Container guide for details.

Creating widgets

Inherit the Yii\base\widget class and override the Yii\base\widget::init () and/or Yii\base\widget::run () methods to create widgets. init()The usual method handles the Widget property, which run() contains the code for the widget to generate the rendered result. Render results can be either run() "echoed" output directly in the method or returned as a string.

The HelloWidget values assigned to the property are encoded in the following code message , and "Hello World" is displayed by default if the property is not assigned a value.

NamespaceApp\components;UseYii\Base\Widgets;UseYii\Helpers\Html;Classhellowidget extends Widget{public $message; public function init () {parent::init (); if ( $this->message = = null) {  $this->message =  ' Hello world ';}} public function run () {return Html::encode (  $this->message); }} 

Using this widget simply uses the following code in the view:

<?phpuse app\components\HelloWidget;?><?= HelloWidget::widget([‘message‘ => ‘Good morning‘]) ?>

The following is another type begin() end() of HTML-encoded content that can be used in and called HelloWidget , and then displayed.

NamespaceApp\components;UseYii\base\widget; Use yii\helpers\Html; class hellowidget extends widget{public  function init () {parent::init (); Ob_start (); } public function run () {$content = Ob_get_clean (); return Html::encode ($content);}}         

As shown above, the PHP output buffers are init() started, and all init() the run() output content between and methods is fetched, and is run() processed and returned.

Note: When you call Yii\base\widget::begin (), a new widget instance is created and the method is called at the end init() of the construct, where the end() method is called and the run() output returns the result.

The following code shows how to use this HelloWidget :

<?phpuse app\components\HelloWidget;?><?php HelloWidget::begin(); ?> content that may contain <tag>‘s<?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, for example:

public function run(){ return $this->render(‘hello‘);}

The widget's view file is stored by default in the WidgetPath/views directory, which WidgetPath represents the directory where the widget class file resides. If the above sample widget class file is @app/components under, 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 resides.

Best practices

Widgets are object-oriented ways to reuse view code.

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.

YII2 Widget (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.