Yii RenderPartial Usage Example

Yii RenderPartial Usage Example

Last updated:

Partial Views (views within a view) can be a nice way to reduce complexity in a view, because you can split specific areas of your view into different php files.

Partial View can also help you increase modularity in your apps. By splitting views into parts, you may find that some parts are common to many views and you can find new ways of composing them and sharing code between your views, helping you add a common OO tenet to your projects (the DRY principle).

Within a normal view, do:

<div class="span2">
    <?php
        echo $this->renderPartial('partial_view_file', [
            'foo' => $my_var,
            'bar'=>$my_other_var
        ]);
    ?>
</div>

alternatively, using php short tags syntax

<div class="span2">
    <?= $this->renderPartial('partial_view_file', [
        'foo' => $my_var,
        'bar'=>$my_other_var
    ]); ?>
</div>

Dialogue & Discussion