Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 Advanced - Extend View To Add Custom Variable

I am not sure if what I am going for is the best Yii solution to handle this. So I am open to better solutions. Whatever is the most Yii way to do it.

I have a left sidebar that only needs to be shown to user's who are logged in. THAT is not the problem, I know how to show something by checking 'Yii::$app->user->isGuest'.

On specific pages, I do not want this sidebar, EVEN if they are logged in. For example, the 'Contact Us' page does not need to have the left sidebar on it. I really don't care if it does have the sidebar, but it is throwing off the Bootstrap3 columns.

<div class="row">
    <div class="col-lg-4 center-col">

        <div class="site-contact">
            <h1><?= Html::encode($this->title) ?></h1>

            <p>
                If you have business inquiries or other questions, please fill out the following form to contact us. Thank you.
            </p>

            <div class="row">
                <div class="col-lg-12">
                    [contact form code truncated]
                </div>
            </div>
        </div>

    </div>
</div>

It looks great, centered in the middle of the page, 4 cols wide.

However, in my Yii main.php (frontend/views/layouts/main.php) I have 2 separate layouts, depending on if the user is logged in.

<div class="container-fluid mainpage">

    <?= Alert::widget() ?>

    <div class="row">

        <?php if (!Yii::$app->user->isGuest) { ?>

            <div class="col-md-3 sidebar">
                <?= Menu::widget([
                    'options' => ['class' => 'nav nav-sidebar'],
                    'items' => [
                            ['label' => 'Home', 'url' => ['site/index']],
                            ['label' => 'About', 'url' => ['site/about']],
                            ['label' => 'Contact', 'url' => ['site/contact']],
                        ],
                    ]);
                ?>
            </div>

            <div class="col-md-9">
                <?= $content ?>
            </div>

        <?php } else { ?>

            <div class="col-sm-12">
                <?= $content ?>
            </div>

        <?php } ?>
    </div>

</div>

This code, shows a full width page if it's a guest, or a sidebar if the user is logged in.

What is happening on pages like the 'Contact Us' page, is the form gets scrunched because of the sidebar. The right content is 'col-md-9', and when you put the contact form into it (having 'col-lg-4') its only taking up 4 inside the 9 columns, resulting in it being scrunched.

Since this page (the contact page) should be publicly viewable (ie: if a user can't login, they would be screwed and not able to contact support), it is possible that both cases could happen. It may be viewed by a logged in member, resulting in the scrunched form, or by a guest that would look fine.


My approach was to add a variable $showSidebar. Then in contact.php have $showSidebar = false. The default action for the variable would be set by isGuest, and the pages could override it to turn off the sidebar on a per page basis.

I really don't want a ton of if checks for isGuest to show alternate div's or code, so I feel this would be the best approach.

How do I extend the View to add my own variable? The file is in vendor/yiisoft/yii2/web/view I think..

This is the Yii2 advanced template.

PS: I am open to a better solution to my problem. I don't want hacks, but the most proper Yii way to do it.

like image 770
Wade Avatar asked Apr 19 '15 05:04

Wade


2 Answers

As the View is a Yii component, you can set it up like other components in the yii config file. Just add sth like the following:

 'components'     => [
        'view'         => [
            'class' => 'app\components\extended\View',
        ],
like image 54
Asped Avatar answered Nov 16 '22 16:11

Asped


I think that the best solution is to create two layouts. One with the sidebar and another without the sidebar and use the desired layout.

like image 40
Szántó Zoltán Avatar answered Nov 16 '22 17:11

Szántó Zoltán