Andrii Kovalenko
Andrii Kovalenko

Reputation: 2227

Yii2: Rendering view from within the widget

What have I tried?

  1. I have created CustomWidget by extending Widget class

  2. In the end of run method body I tried to place by different ways calling render method.

  3. I have tried this ways

$this->render( '//widgets/custom' )

$this->render( 'custom' )

$this->render( '@frontend/views/widgets/custom' )

$this->renderFile(/*absolute path to file*/)

But all in vain.

And with another view, for example

$this->render('//site/404') - Works

I thought maybe the problem the widget is searching for different directory than all other views. I have overridden Widget method getViewPath and intentionally return path to my views. By default getViewPath returns .../components/views. But the error is still exists. I have tried to place widget directory to components/views, but there is no result.

Piece of CustomWidget code

class CustomWidget extends Widget {
    public function run() {
        return $this->render( '/widgets/custom' );
    }
}

I'll be grateful for any clue, suggestion, advice about fixing this problem!

Upvotes: 2

Views: 1580

Answers (4)

It is very easy actually:

return (new CommentsWidget())->render('_some_widget_view_name', [
    'comments' => $activeDataProvider
]);

Upvotes: 0

Ricardo Grana
Ricardo Grana

Reputation: 1

render does not recognize relative paths that are not relative to its respective controllers, unless you use an alias.

So, if you want to use a path relative to a component or widget, for example:

widgets/MyWidget/MyWidget.php

widgets/MyWidget/views/custom.php

Use this inside MyWidget.php:

$this->renderFile(dirname(__FILE__). '/widgets/custom.php' )

Upvotes: 0

Andrii Kovalenko
Andrii Kovalenko

Reputation: 2227

The problem is ftp and php users have different groups.

I created file via ftp and the file has different group than php user started with.

Changing group for the widgets/custom.php and group for directory widgets to appropriate to php and set permission to 0770 fixed that issue.

Upvotes: 0

Muhammad Omer Aslam
Muhammad Omer Aslam

Reputation: 23738

You can render a view like below inside a widget, i am using the views/site/about.php view in the following example

echo \Yii::$app->view->render('@app/views/site/about');

You can use aliases or just /site/about in the above line too, but mind the starting / which is important in case you are trying to load a view from the application's view folder otherwise it will point to the current view folder where the widget is loaded.

Yii2 list of path aliases available with basic and advanced-app.

Yii2 Basic App

  • @app: Your application root directory
  • @vendor: Your vendor directory on your root app install directory
  • @runtime: Your application files runtime/cache storage folder
  • @web: Your application base url path
  • @webroot: Your application web root
  • @tests: Your console tests directory

Yii2 Advanced App

  • @app: Your application root directory (either frontend or backend or console depending on where you access it from)
  • @vendor: Your vendor directory on your root app install directory
  • @runtime: Your application files runtime/cache storage folder
  • @web: Your application base url path
  • @webroot: Your application web root
  • @tests: Your console tests directory
  • @common: Alias for your common root folder on your root app install directory
  • @frontend: Alias for your frontend root folder on your root app install directory
  • @backend: Alias for your backend root folder on your root app install directory
  • @console: Alias for your console root folder on your root app install directory

Upvotes: 2

Related Questions