CodeIgniter Forums
Dynamic Header That Needs A Different HTML Structure - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Dynamic Header That Needs A Different HTML Structure (/showthread.php?tid=38742)



Dynamic Header That Needs A Different HTML Structure - El Forum - 02-17-2011

[eluser]piehouserat[/eluser]
I currently have something that looks like this

Code:
function index()
    {
        $header_data['page_title']    = 'Home';
        $this->load->view('includes/header', $header_data);
        
        if (!$this->tank_auth->is_logged_in()) {
            $this->load->view('not_logged_in');
        } else {
            $data['user_id']    = $this->tank_auth->get_user_id();
            $data['username']    = $this->tank_auth->get_username();
            $this->load->view('logged_in', $data);
        }
        
        $footer_data['copy_year']    = date('Y');
        $this->load->view('includes/footer', $footer_data);
    }

not_logged_in view

Code:
Already a member? <?php echo anchor('/auth/login/', 'Login'); ?>

logged_in view

Code:
Hi, <strong>&lt;?php echo $username; ?&gt;</strong>! You are logged in now. &lt;?php echo anchor('/auth/logout/', 'Logout'); ?&gt;

What I want to do is move the logged in/out view into the header view. The only way I can think of doing this is to create two seperate header views - 1 for logged in users and 1 for those not logged in. This would mean that I would have 2 copies of the same header view with only a few small differences shown above between the two. This would increase code maintenance and doesn't seem like the most elegant solution.

I'm just starting out with CodeIgniter so perhaps I'm thinking about this the wrong way but there must be a way of having just one header view that can change html structure based on application logic.


Dynamic Header That Needs A Different HTML Structure - El Forum - 02-17-2011

[eluser]steelaz[/eluser]
You can run

Code:
$this->tank_auth->is_logged_in()

in your view file, not just controller.


Dynamic Header That Needs A Different HTML Structure - El Forum - 02-17-2011

[eluser]piehouserat[/eluser]
Ah I didn't think about that because I thought all logic like that was done in the controller? Well my controller now looks like this:

Code:
function index()
    {
        $header_data['page_title']    = 'Home';
        
        if (!$this->tank_auth->is_logged_in()) {
            $this->load->view('includes/header', $header_data);
        } else {
            $header_data['user_id']    = $this->tank_auth->get_user_id();
            $header_data['username']    = $this->tank_auth->get_username();
            $this->load->view('includes/header', $header_data);
        }
        
        $footer_data['copy_year']    = date('Y');
        $this->load->view('includes/footer', $footer_data);
    }

and I've modified my header view to contain this:

Code:
...
        <ul id="nav">
            &lt;?php
            if (!$this->tank_auth->is_logged_in()) {
            ?&gt;
            <li id="t-signin">
                &lt;?php echo anchor('/auth/login/', 'Sign in'); ?&gt;
            </li>
            &lt;?php
            } else {
            ?&gt;
            <li id="t-profile">
                <a href="/&lt;?php echo $username; ?&gt;" class="url" rel="contact">You</a>
                <ul class="tabs">
                    <li>&lt;?php echo anchor('/' . $username, 'Your profile'); ?&gt;</li>
                    <li>&lt;?php echo anchor('/account', 'Your account'); ?&gt;</li>
                    <li>&lt;?php echo anchor('/auth/logout/', 'Sign out'); ?&gt;</li>
                </ul>
            </li>
            &lt;?php
            }
            ?&gt;
...

And this all works well Smile
But is this good MVC architecture? I thought all application logic was meant to be kept out of the view and in the controller?

Thanks again


Dynamic Header That Needs A Different HTML Structure - El Forum - 02-17-2011

[eluser]steelaz[/eluser]
That's why I like CodeIgniter, it's not enforcing strict MVC.

In this case, I would call it view logic and not application logic, you're just deciding what part of view to show based on some condition.

You can also create $logged_in variable in controller, assign $this->tank_auth->is_logged_in() value to it and pass it to your view, so you could:

Code:
<ul id="nav">
    &lt;? if (!$logged_in): ?&gt;
        <li id="t-signin">
            &lt;?php echo anchor('/auth/login/', 'Sign in'); ?&gt;
        </li>
    &lt;? else: ?&gt;
        <li id="t-profile">
            <a href="/&lt;?php echo $username; ?&gt;" class="url" rel="contact">You</a>
            <ul class="tabs">
                <li>&lt;?php echo anchor('/' . $username, 'Your profile'); ?&gt;</li>
                <li>&lt;?php echo anchor('/account', 'Your account'); ?&gt;</li>
                <li>&lt;?php echo anchor('/auth/logout/', 'Sign out'); ?&gt;</li>
            </ul>
        </li>
    &lt;? endif; ?&gt;
</ul>



Dynamic Header That Needs A Different HTML Structure - El Forum - 02-18-2011

[eluser]Basketcasesoftware[/eluser]
Well if you are concerned about it views allow the passing in of variables which will be substituted. Or you can use the Parser class and use meta-variables. In that case the controller will be doing all the work and the view will just be a display string.