Welcome Guest, Not a member yet? Register   Sign In
Best practice to including sidebar on some pages, but not all?
#1

[eluser]jleequeen[/eluser]
In my app, I need some pages to include a sidebar and some that need the full screen width. Currently I'm using a layout as suggested in these forums with a main template file with a placeholder variable that I fill in the content with in my controller. I'm trying to figure out the best way to include a sidebar, but that doesn't need to be there in every page. Some individual pages need a specific sidebar, and others need to share a similar sidebar. Then I have pages that need the whole screen with no sidebar.

Any best practice out there to doing this?
#2

[eluser]ELRafael[/eluser]
[quote author="jleequeen" date="1257543772"]In my app, I need some pages to include a sidebar and some that need the full screen width. Currently I'm using a layout as suggested in these forums with a main template file with a placeholder variable that I fill in the content with in my controller. I'm trying to figure out the best way to include a sidebar, but that doesn't need to be there in every page. Some individual pages need a specific sidebar, and others need to share a similar sidebar. Then I have pages that need the whole screen with no sidebar.

Any best practice out there to doing this?[/quote]

you can use helper.
something like that:

Code:
echo sidebar();

Inside the helper you can check if the page will be full width or not (an array with all pages that will have sidebar. you can put this in config.php or in a database)
#3

[eluser]Burak Guzel[/eluser]
Pass a variable named sidebar to your view:

Code:
// ...
$data['sidebar'] = 'mysidebar';
$this->load->view('page',$data);

In your page view:

Code:
<?php if (isset($sidebar)) $this->load->view($sidebar); ?>

So you can tell your page views which sidebar to load, or not.
#4

[eluser]ELRafael[/eluser]
[quote author="lane4" date="1257561261"]Pass a variable named sidebar to your view:

Code:
// ...
$data['sidebar'] = 'mysidebar';
$this->load->view('page',$data);

In your page view:

Code:
<?php if ($sidebar) $this->load->view($sidebar); ?>

So you can tell your page views which sidebar to load, or not.[/quote]

be carefull with this.
if you don't set $data['sidebar'] in your controller, when you try to call a non-defined var in your view, maybe php shows a warning.

imo
In you page view
Code:
<?php echo (isset($sidebar)) ? $sidebar : ''; ?>
#5

[eluser]Burak Guzel[/eluser]
My mistake, meant to type

Code:
if (isset($sidebar)) $this->load->view($sidebar);
#6

[eluser]jleequeen[/eluser]
Thanks for your suggestions. I understand how that would work. My problem is, when I want to switch from a full column to a smaller column with a sidebar wouldn't I need to also change my css? Do you have some decision logic to show different main column div's if you want a full width view versus a smaller one with a sidebar?
#7

[eluser]Burak Guzel[/eluser]
Maybe make your sidebar float to the right using css. Main div should be able adjust.

Code:
.sidebar {
    float: right;
}
#8

[eluser]ELRafael[/eluser]
what i do in my projects:
use js to check if there is sidebar content. in jquery, something like this
Code:
var has_sidebar = $('div.sidebar_class').html();
if (has_sidebar.length > 1)
{
  $('div.sidebar_class').addClass('some_class');
  $('div.right_content_of_sidebar').addClass('some_other_class');
}

but there are a thousand solutions! even with ci.

the js maybe wrong, i don't check nothing Big Grin
#9

[eluser]jleequeen[/eluser]
The requirements of the app i'm working on unfortunately prohibit me from using javascript to be able to function. I can go in later and add some javascript to pretty things up, but it can't be used for anything that would break if you turned it off.
#10

[eluser]andrewtheandroid[/eluser]
[quote author="jleequeen" date="1257563453"]Thanks for your suggestions. I understand how that would work. My problem is, when I want to switch from a full column to a smaller column with a sidebar wouldn't I need to also change my css? Do you have some decision logic to show different main column div's if you want a full width view versus a smaller one with a sidebar?[/quote]

Hey jleequeen I use the above approach mentioned. In my template inside the header I allow for additional stylesheets to be called so a particular view can overide the global.

Inside template
Code:
<html>
<head>
<? if(isset($stylesheets)): // Check for specified sheets
foreach($stylesheet as $sheet): // Create the link for each one ?>
<link href="<?=base_url()?>css/<?=$sheet?>.css" ....etc>//Sheet is the filename
<? endforeach; endif; ?>
</head>

<body>
  <div id="sidebar"></div>// iffset sidebar as above
  <div id="content"></div>// iffset content (if not show default content
&lt;/body&gt;
&lt;/html&gt;

then as above with the $data['content'] I also set
Code:
// Pass stylesheets into override style
$data['stylsheets'] = array('mystyle','secondStylesheet','morestylesheet');
then pass this into the view with the content.

Then you could use css to style the sidebar to your liking.




Theme © iAndrew 2016 - Forum software by © MyBB