• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Theory - Extension to the MVC model, "Template"

#1
[eluser]Unknown[/eluser]
I've been using CI for a while now. I've iterated through the same project about 5 times. Each time I started over, I moved closer and closer to current set up I'm using right now.

That set up uses additional models to help construct layouts, or to be more specific, pieces of layouts, then sends them to the controller to be put together.

My question is, should this be built upon, or at least discussed? Or is there something inherently wrong with how I chose to do it?

Everyone knows, models are supposed to be the data. So, that means there REALLY should be another layer that comes between Controller and Model interaction. Something like MVCT(template)

Here's an example:

In my controller, rather than build my entire page in `index` I build specific pieces using `components_m` then put it together in a template view.
Code:
function index($uri1)
        {
                $data['user'] = 'someinput'.$frompage.$uri1;
                $page['header'] = $this->components_m->header($data);
                $page['content']  = $this->components_m->index($data);
                $page['footer']  = $this->components_m->footer($data);
                $this->load->view('template' ,$page);
        }

components_m->header would consist of all the pieces that would make up the header view. Same with content/footer.

The template, would be as simple as :

Code:
<div class='header'>&lt;?=$header?&gt;</div>
<div class='content'>&lt;?=$content?&gt;</div>
<div class='footer'>&lt;?=$footer?&gt;</div>

This is abstracted more once you get into components_m->header()

Code:
public function header($data = '') {
            $data['active'] = $this->session->userdata('active');
            $data['sharedinfo'] = $this->a_model->getdata($data);

            $page['active']         = $this->header_m->active($data);
            $page['search']         = $this->header_m->search($data);
            $page['filters']        = $this->header_m->filters($data);
            $page['newthread']      = $this->header_m->newthread($data);
            return $this->load->view('header', $page, TRUE);
        }

To help with organization, I built 2 layers of templates. The first, components_m, represents all the different sections of the page. It allows us to easily place the header code on any page, or any unique template through, one line: $this->components_m->header();

The second layer consists of multiple models: header_m, index_m, footer_m, and allows you to further organize and modularize each piece of an individual component. So for instance, if I use AJAX, and I only need to update a specific piece of 'header,' for example, 'active.' I can just run: $this->header_m->active($data), and replace on the page as necessary.

I organize my views in the following manner:

views
- header
- - active.php
- - search.php
- - filters.php
- - newthread.php
- footer
header.php
footer.php

header.php would look similar to the template, only just for the header components

Code:
<div class='active'>&lt;?=$active?&gt;</div>
<div class='search'>&lt;?=$search?&gt;</div>
etc

Now, this works for me, but I'd be curious if it brings up any specific problems, or if it could be optimized. I don't like using models for templating. There should really be something else to help.

Sorry if I come across as ignorant, I only started programming 1.5 years ago. Although, I did want to at least get this out there and see what other more experienced programmers thought.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.