Widgets
In Yii2, widgets are reuseable code blocks that are used in views to add configurable user interface logic to our application in an object-oriented way. Yii2 comes with several different types of reusable widgets, some of which we have already seen in previous chapters. Custom widgets can also be created to create tools that can be reused across multiple projects. In this section, we'll go over the basic types of widgets, how to use them, and how to implement our own within our applications.
Using widgets
As a presentation layer tool, widgets are most commonly used within our view files. In Yii2, widgets can be used in one of two distinct ways. The first way to use widgets is to call the yii\base\Widget::widget()
method on a supported widget within a view. This method takes a configuration array as an option and returns a rendered widget as a result. For instance, to display a Twitter Bootstrap 3 style alert on our page, we can use the yii\bootstrap\Alert
widget as follows:
<?php...