Can someone explain to me how widgets work/render?

Hi folks,

I am hoping someone can help me to properly understand how widgets work.

I’ve been looking at the ActiveForm widget as an example. Here is it’s representation in the view file: -

<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>

	<?= $form->field($model, 'name') ?>

	<?= $form->field($model, 'email') ?>

	<?= $form->field($model, 'subject') ?>

	<?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>

	<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [

		'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',

	]) ?>

	<div class="form-group">

		<?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>


<?php ActiveForm::end(); ?>

How is all this actually being processed? I would consider myself to have a pretty good grasp of PHP/web technologies, but I think this is going over my head still.

I’ve had a close look at the class files for this widget and I can see


is returning an object but that the view file also contains HTML between ActiveForm methods. If


was simply echoing back HTML, I could understand it but it seems to me that Yii (or the widget itself to be specific) seems to be stacking output (by ob_start()?)?

Could anyone offer me some information on what is happening here? It’s one of the parts of PHP/Yii that I still don’t have a complete technical understanding of and I would like to properly understand what is going on…

Thanks in advance! :) :) :)


You’re on the right track. Widgets are implemented using output buffering via ob_*.