简体   繁体   中英

yii widget render partial view of another controller

I need to create universal dialog widget for different modules in my yii-powered project, with form which will be auto serialized and passed to controller action for add/update operations.

Is it a good practice if I call render partial method in overloaded run() or init() method of my widget and pass him custom view path with html form (this form will be shown in modal dialog window after dialog-btn click action accordingly)?

My widget path:

/protected/components/widgets/DialogFormWidget.php

My forms path:

/protected/modules/module/controller/views/forms/submittable_html_form.php

go with

 $this->renderPartial('application.modules.controllers.views.forms.submittable_html_form');

UPDATE :

in above code, $this refers to the controller, if you want to use controllers renderPartial from inside a widget, you have to access it like $this->controller->renderPartial (

Even you can use path to your view file to access it from other controller ($this->renderPartial('application.views.controller.view', []) , or path to any place where your view live on system), this is not good solution. Better solution is to create Yii widget so u can easy call it from all places because its controller independent. You will have also lot of benefits since with Yii widgets u have option to send some widget configuration parameters that can be used to customize widget and even load different views/style based on that parameters. Also you will have good place for custom functions that can help to add some business logic to your view and similar. You can find some start point for this here ( http://www.yiiframework.com/doc/guide/1.1/en/basics.view )

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