简体   繁体   中英

Yii2 - Render form inside footer (main.php)

in my main.php layout file, of Yii2, i need to render a form located in folder contacto/_form.

How can i pass the $model variable to the file main.php, inside layouts folder, and use it in:

<?= $this->render('_form', [
    'model' => $model,
]) ?>

Many thanks.

You can create widget like that:

class FormWidget extends Widget
{
    /**
     * @return string
     */
    public function run()
    {
        $model = ...;// code to create model

        return $this->render('_form', [
            'model' => $model
        ]);
    }
}

And in layout input widget like that:

<?= FormWidget::widget() ?>

For more read for create widgets - http://www.yiiframework.com/doc-2.0/guide-structure-widgets.html#creating-widgets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM