Multiple views and templating with CodeIgniter: Method 2

When we last left off...

In the previous episode, we left off with the basic instructions for setting up a templating system in your CodeIgniter application. The benefits are two-fold:

  • It saves you lines of code when using many views
  • It helps with future maintenance by abstracting away a great deal of your code

So, onto the new stuff...

With that said, lets move onto an interesting situation. Let's suppose you've set up a view which has a header, navigation, content, sidebar, ad unit, and a footer.

So, your view, with all the includes, would look something like this:

<$this->load->view('partials/header');>
<$this->load->view('partials/nav');>

<div id="content">

<h2>Some content here.</h2>

<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam hendrerit orci commodo tortor. Nam eu lacus in turpis placerat pulvinar. Mauris vehicula nisi posuere magna. Aliquam a purus quis pede volutpat commodo. Fusce rutrum tortor non massa. Quisque non tortor. Duis adipiscing pretium velit. Maecenas a ante. Vivamus id urna. Nulla facilisi.</p>
</div>

<$this->load->view('partials/sidebar');>
<$this->load->view('partials/ad');>
<$this->load->view('partials/footer');>

And the controller index function would simply be this:

function index ()
{
<$this->load->view('home');>
}

Notice that I obviously am keeping all the sub-views, or partials, in a folder together. This is just for organizational purposes, and you don't really have to do it...but you should wink

Disclaimer!! Important!!

Now, before all you MVC crazies out there start hammering me with complaints like "You shouldn't have logic in the view" and "You're breaking MVC and promoting bad programming" let me set one thing straight. The logic that will be in the view is display logic. The "V" in MVC is for the view, or presentation of data. We will be actually making the decision in the controller. So the actually programmatic logic is, in fact, in the controller. However, the result of said logic happens in the view, as it should. So, disclaimer over, lets move on, shall we?

Moving on...

Okay, adding a little logic to our view; here's the situation: You have this sidebar view and this ad view. We're going to set up a little logic to let the view decide when the ad gets loaded into the view. The reason could be that you have a public site that serves an ad, but logged-in, registered users get an ad-free experience. Regardless, the point is, you can see how this works with this example.

You can easily extend the logic to say that non-logged in users get an ad and a sidebar, but logged in users get a control panel and a sidebar. Of course, you'd want to make sure for your layout, spacing, and all that you use classes. That way, when you replace something in the sidebar, its fluid and consistent. See how useful this can be?

Something very basic...

Okay, for now, lets start with something basic. We're going to set up an array called data, and we will assign an item in that array. The key will be "loggedIn" and we will manually set the value to "true."

In the controller, you'll have this:

function index ()
{
$data['loggedIn'] = 'true';
$this->load->view('home', $data);
}

And in the view, you'll put this around your call for the ad:

<?if ($loggedIn != "true") { ?>
<?=$this->load->view('partials/ad')?>
<? }?>

Now, what you'll notice is since we set the value to "true," the ad does not load! However, go back in the controller and change the value to "false" or, better yet, leave it empty. Now you will see the ad presented.

Okay, where's this "extensible" bit? Right here...

Remember I told you we could extend this? Okay, let me show you how to do that.

First, lets add another view file called "admin.php," and add a call for it in the view:

<?=$this->load->view('admin')?>

Now, we're going to expand on the logic that we put in the view. What we want is instead of a non-logged in user seeing an ad, we want the logged-in users to see their little control panel. It should look like this:

<?if ($loggedIn != "true") {?>
<?=$this->load->view('partials/ad')?>
<?} else {?>
<?=$this->load->view('partials/admin')?>
<?}?>

Now, if you have the value set to "true" you will see the sidebar and your admin panel. And if you are not logged in, you will see the sidebar and an ad.

In closing...

So, this concludes our discussion on view logic. As you can see, this method is very flexible. If you use your imagination, I'm confident you can extend this technique much, much further.

In the next article in the series, I'll describe another method that allows for controlling all this decision logic inside the controller.

Posted by MtheoryX on 11/24 at 11:41 PM
Commenting is not available in this weblog entry.

<< Back to main