Yii v2 snippet guide III

Comparing #82 with #83

Revision #83 was created by rackycz rackycz on Apr 1, 2021, 10:29:50 PM.

Custom Widgets

Content

[...]
- It means you forgot to copy some PHP code from the old layout file to the new one.

Now you are done, you can start using HTML and JS stuff from AdminLTE. So lets check extensions which will do it for us

**- Insolita extension**
--
--

Works good for many UI items: Boxes, Tile, Callout, Alerts and Chatbox.
[...]
```
And this is basically it. Now we know how to use AdminLTE and fix the GridView. At least one extension will be needed to render widgets, see above.

 
 
**Creating custom Widget**
 
--
 
See official reading about [Widgets](https://www.yiiframework.com/doc/guide/2.0/en/structure-widgets) or this [explanation](https://www.codevoila.com/post/33/yii2-example-create-yii2-custom-widget-advanced-part). I am presenting [this example](http://www.bsourcecode.com/yiiframework2/how-to-create-custom-widget-in-yii2-0-framework/), but I added 3 rows.
 
 
Both types of Widgets can be coded like this:
 
 
```php
 
namespace app\components;
 
use yii\base\Widget;
 
use yii\helpers\Html;
 
 
class HelloWidget extends Widget{
 
 public $message;
 
 public function init(){
 
  parent::init();
 
  if($this->message===null){
 
   $this->message= 'Welcome User';
 
  }else{
 
   $this->message= 'Welcome '.$this->message;
 
  }
 
  // ob_start();
 
  // ob_implicit_flush(false);
 
 }
 
 public function run(){
 
  // $content = ob_get_clean();
 
  return Html::encode($this->message); // . $content;
 
 }
 
}
 
 
// This widget is called like this:
 
echo HelloWidget::widget(['message' => ' Yii2.0']);
 
 
// After uncommenting my 4 comments you can use this
 
HelloWidget::begin(['message' => ' Yii2.0']);
 
echo 'My content';
 
HelloWidget::end();
 
```