Sidebars in Yii

What is the best way to create sidebars in Yii that may vary from page to page?

Create a widget just like CMenu. You can pass any parameters to this widget from a controller and thus change its behavior and presentation depending on action.

Is this the best way yeah?

Do you mean create a CMenu widget?

Or create my own widget that is like CMenu?

Everything depends on what you want to see in your sidebar. Widget is just a part of a page, which can vary from action to action. I’m sure you won’t be happy to see php ifs and elses in a layout file.

Correct, so I will widget which is defined in the controller.

Just out of curiosity, anyone else got any sidebar ideas?

Eee… Am I missing something, or do you have sidebars perfect example in any basic webapp generated with yicc webapp after using gii tool for generating any model CRUD operations? :)

Just follow Creating First Yii Application in Definitive Guide, especially chapters “2. Implementing CRUD Operations” and then “Accessing CRUD Pages” and after login you have a beautiful example of side bar, titled “Operations”. Can’t be easier in my opinion! :)

Cheers for the advice.

A ‘sidebar’ in Yii is just telling your app to use a different layout. :)

I am curious:

How on Earth did you start your Yii app?

Didn’t you create it using the command line?

If you did, then you would’ve ended up with sidebars.

This is what’s in views/layouts/column1.php:

<?php $this->beginContent('//layouts/main'); ?>

	<div id="content">

		<?php echo $content; ?>

	</div><!-- content -->

<?php $this->endContent(); ?>

That’s the no-sidebar layout.

This is the ‘sidebar-on-the-right’ layout, column2.php:

<?php $this->beginContent('//layouts/main'); ?>

<div class="span-19">

    <div id="content">

        <?php echo $content; ?>

    </div><!-- content -->


<div class="span-5 last">

    <div id="sidebar">

All the stuff in the sidebar goes here.

    </div><!-- sidebar -->


<?php $this->endContent(); ?>

In your controllers, there’s different ways to set the layout.

By setting the controller layout variable:

	public $layout='//layouts/column1';

Or by setting the layout in your controller actions:

        $this->layout = '//layouts/column1';

I plan to use several layouts, so I’ll probably make a column3.php and maybe a special.php …

If you want to know more about how to make your own layouts, you need to look into the BluePrint CSS framework.

Here’s a list of tutorials:

R U sure, jacmoe? :)

Sidebars become available (visible), if you use Gii to generate model and CRUD operations for it and then login as admin and go to page that uses that CRUD operations. If you only generate a freshly new, out-of-box application with using yiic webapp and don’t hire Gii to make model and CRUD, you won’t see sidebars at all.

Although, you’ll see code parts prepared for sidebars, as you showed examples.

Have I feed your curiosity? :)

Well, then… I didn’t think of that. You have a point.

So, to the OP:

Just put this code in the actions you want to be with a sidebar:

$this->layout = '//layouts/column2';

>>$this->layout = ‘//layouts/column2’;<<

I do the same thing as jacmoe.

Why don’t you try this sidebar extension. :rolleyes: