Yii 2: Load a module's view as a partial view on another application view

1.6k views Asked by At

I need to load a module's view as a partial view inside another view of the application. I find no clue about how to do this in the manual.

The view is completely independent of the module:

<?php
    // This is the module's class. Do I need it here?
    use vendor\xxx\cropk\CropK;

/* @var $this yii\web\View */
    $this->title = 'Cropping Test';
?>
<div class="site-index">
    <p>Cropping Test</p>
    <?php
        // ...
    ?>
</div>

How can I accomplish this?

1

There are 1 answers

1
Kirk On BEST ANSWER

Looking at render's documentation you have a few options:

The view to be rendered can be specified in one of the following formats:

  • path alias (e.g. "@app/views/site/index");
  • absolute path within application (e.g. "//site/index"): the view name starts with double slashes. The actual view file will be looked for under the view path of the application.
  • absolute path within module (e.g. "/site/index"): the view name starts with a >single slash. The actual view file will be looked for under the view path of $module.
  • relative path (e.g. "index"): the actual view file will be looked for under $viewPath.

Based on those choices, it looks like you will specify the absolute path within application, or create a path alias and use that syntax (app? main site view? wherever it's at.)

So, if you wanted to render the {basePath}/views/site/my_partial.php you'd do something like $this->renderPartial('//site/my_partial.php'); in your view.