Displaying views elsewhere in Yii2 - no renderPartial?

I need to display a partial view inside the component's user file in Yii2 and according to the Yii docs you can access the view instance through something like this:

 \Yii::$app->view->renderFile('@app/views/site/license.php'); 

I went ahead and tried:

 Yii::$app->view->renderPartial('//my/view/'); 

... but then received an error message that I was trying to access a nonexistent method.

Then I checked the view class and noticed that it does not have renderPartial , and this is the controller method.

I see that it has a renderFile method and a render method; which one should i use?

The docs don't indicate that the render method includes a layout, such as a method with the same name, from a controller class, so I'm not sure; as for renderFile I'm not 100% sure if this fits or?

Can someone explain which method will produce the same results as renderPartial ?

+6
source share
1 answer

You can call renderPartial from Yii::$app->controller->renderPartial('myview'); Also, as you can see from the source code yii \ base \ Controller renderPartial call to the View render method is called, so you can use Yii::$app->view->render . Basically, there is no difference between render and renderFile , because render internally calls renderFile . But when you use render , you can pass $view in several formats, such as the path alias, the absolute path in the application or the whithin module and the relative path. And before renderFile you can only pass an absolute file path or path alias.

+20
source

All Articles