MaxQ
MaxQ

Reputation: 605

Composite views with Codeigniter?

For my Codeigniter site, I started by making a view for each controller situation. This was impractical, as it would require going back to the code for each to make a change. So I changed approach and operated on a 'default' controller with optional fields. I then thought I could load special views as needed into it.

I put together this view with optional fields with fields for $title, $search_bar on/off etc. However, now came the content area. I was able to load more views into this default view using:

        $data['content_views'][]='blocks/login';

        $this->load->view('default/a', $data);

and in the 'default'view:

if(isset($content_views)&& (is_array($content_views)))
    {
        foreach($content_views as $content_view)
        {
            $this->load->view(&$content_view);
        }
    }

(and that works fine) Two questions:

  1. Am I making things to complex? Is this an accepted way of doing this? Or have I misunderstood the functioning of a view and how they are intended to work?

  2. I want a way to mix the $content_view, i.e. a block of text, then a view. I'm not quite sure as to how to proceed. Say I want a message first, then a view, then more text. This method will only accept views.

Can anybody help me create this flexible approach?

Upvotes: 1

Views: 598

Answers (1)

Kai Qing
Kai Qing

Reputation: 18843

Yeah I would say you're making things a little complex. While I may not be following your description well enough to know precisely how to respond, I can tell you how I do it:

First the whole site is run through a template so the header and footer are the container file and all views needed within the site are rendered as page type views - like an article page, a gallery page, etc. Components are loaded and passed to the views as strings:

$content['sidebar'] = $this->load->view('components/sidebar', $data, true);

That last true says to render as string.

Essentially, this means the page views are pretty much html with php echoing out the necessary elements. No views calling other views, which is how I read your example.

CI loads views progressively, so your controller can output like so:

$this->load->view('header', $header_data);

$view_data['sidebar'] = $this->load->view('components/sidebar', $sidebar_data, true);
$this->load->view('content', $view_data);

$this->load->view('footer', $footer_data);

and in content view, handle the sidebar like so:

<?php if(isset($sidebar)): ?>
<nav>
    <?php echo $sidebar; ?>
</nav>
<?php endif; ?>

And, assuming you populate those arrays for each view it will render header, then content, then footer. And also render sidebar if it is present.

So combining everything, I'm basically saying you can load in sections in your controllers progressively, passing sub-views as strings to whichever section makes sense. That keeps your view controlling in the controller where it belongs and not in the view files themselves. In my experience, I have not had to write a site that was so complex that this construct wasn't perfectly suitable if the site is planned well.

Upvotes: 1

Related Questions