Welcome Guest, Not a member yet? Register   Sign In
Setting up a dynamic menu in CI
#5

[eluser]wowdezign[/eluser]
Hi Ben,

I normally use a layout - or template - file in my projects. The way I do it seems to work ok for me.

Basically, I use variables in the controller to hold view filenames and then pass the variables into my template view, I'll explain it using the content area as an example, but you could do it with a menu, table, or sidebar too.

1) I create a view of how the page is suppose to look and name it template.php
Code:
<html>
    <head>
        <title>My HTML Template File</title>
    </head>
    <body>
        <div id="headerDiv">SomeSite.com</div>
        <div id="navDiv">
            <ul>
             <li>Nav Item</li>
             <li>Nav Item</li>
             <li>Nav Item</li>
             <li>Nav Item</li>
             <li>Nav Item</li>
            </ul>
        </div>
        <div id="contentDiv">

            
            // Use a variable to hold the name of the file you wish to load
            &lt;?php $this->load->view($contentView); ?&gt;


        </div>
        <div id="footerDiv">&copy; 2009 | All rights reserved.</div>
    &lt;/body&gt;
&lt;/html&gt;


Notice in the code above, instead of loading in a file name, I load in the contents of a variable called "$contentView".


2) In the controller, when I am setting up my data array, I set that variable based on what I want to load like so:

Code:
class SomeController extends Controller{
    
    function SomeController(){
        parent::Controller();
    }
    
    function index(){
        $this->load->model('Page'); // Or whatever model you use to retrieve content
        if($this->Page->getPageType() == 'products'){

            // Name of view file I want to display in the view
            $data['contentView'] = "product-list";

            $data['content'] = $this->Page->getProductsList();
        }else{

            // Name of view file I want to display in the view            
            $data['contentView'] = "standard"; // If it's a regular content page

            $data['content'] = $this->Page->getStandardPageText();
        }
        $this->load->view('template',$data); // Pass in the data array
    }
}

The above example is oversimplified to avoid having to write more lines of code. As you can see, though, when I set the "$data['contentView']" string variable in the controller, it is available in the view.

In the view, you just load it with &lt;?php $this->load->view($contentView); ?&gt;. Te view loads the file with the name that matches whatever string it read from the "contentView" variable.

I use this way of templating because it seems simple to me, I am sure there are other ways of doing it, but I haven't experimented with any others.

I hope this helps.


Messages In This Thread
Setting up a dynamic menu in CI - by El Forum - 07-25-2009, 10:20 PM
Setting up a dynamic menu in CI - by El Forum - 07-26-2009, 05:50 AM
Setting up a dynamic menu in CI - by El Forum - 07-26-2009, 05:57 AM
Setting up a dynamic menu in CI - by El Forum - 07-26-2009, 06:00 AM
Setting up a dynamic menu in CI - by El Forum - 07-26-2009, 06:17 AM
Setting up a dynamic menu in CI - by El Forum - 07-26-2009, 06:40 AM
Setting up a dynamic menu in CI - by El Forum - 07-26-2009, 09:48 AM
Setting up a dynamic menu in CI - by El Forum - 07-26-2009, 11:49 AM
Setting up a dynamic menu in CI - by El Forum - 07-26-2009, 09:29 PM



Theme © iAndrew 2016 - Forum software by © MyBB