Welcome Guest, Not a member yet? Register   Sign In
How to assign different views from a single controller
#1

[eluser]Unknown[/eluser]
Hello. Armed with the knowledge I gleaned from Thomas Myer's Professional CI book, I'm currently building my first CI app (and this is my first post).

I've run into a quandry that I'd like to get some advice on. First off, I am not the most experienced PHP coder out there so I apologize in advance if my comprehension of things seems rudimentary.

My app up to this point is based on much of the code from the book with some of my own original code that I've subsequently written. So I have a single controller that includes a method specifically for my pages:
Code:
function pages($path) {
//use for pages
$page = $this->MPages->getPagePath($path);
$data['main'] = 'page';
$data['title'] = $page['name'];
$data['page'] = $page;
$this->load->vars($data);
$this->load->view('template');
}

The page method pulls the page path
Code:
getPagePath
and all content from the Pages table in the database. The problem I am running into is I have a couple of pages that need to use a completely different view... it is loading
Code:
$this->load->view('template');
as the view for all pages. How would I go about applying a different view to a specific page? For example I have a page called eight-steps.html in my Pages table that needs to have a different view applied. What would the procedure be for accomplishing this? Am I thinking about this wrong? Is there a best practice way to do this?

Any advice will be greatly appreciated. Thanks...
#2

[eluser]Colin Williams[/eluser]
You need to store the template to use in the database. Then your getPagePath method needs to return that value, and then do something like this

Code:
function pages($path) {
//use for pages
$page = $this->MPages->getPagePath($path);
$data['main'] = 'page';
$data['title'] = $page['name'];
$data['page'] = $page;
$this->load->vars($data);
if ( ! $page['template'])
{
  $page['template'] = 'template'; // Use default
}
$this->load->view($page['template']);
}
#3

[eluser]Unknown[/eluser]
Colin... Thanks for the response. I think I follow you for the most part. My initial thought was that including a simple if statement would accomplish this but what didn't occur to me was having to change my getPagePath method. Here is that method:
Code:
function getPagePath($path){
    $data = array();
    $this->db->where('path', db_clean($path));
    $this->db->where('status', 'active');
    $this->db->limit(1);
    $Q = $this->db->get('pages');
    if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
}


I'm not really sure what I would have to add to this method to get the intended result.

So let me just write this out to make sure we are on the same page (no pun intended)...

I'd need to create a new column in my Pages table to store the name of the template I'd like to assign to a specific page. So for example, my page path named eight-ways in my table I'd want to match with a view called eight-ways-template.php. Wait I think I'm more confused than I thought. I keep rereading my page controller method you rewrote but I'm not understanding. Could you clarify a bit more?

BTW, I came across your Template Library yesterday while trying to wrap my brain around this issue and I must say that I will definitely be giving it a spin on my next project!

Thanks...

-Stevie




Theme © iAndrew 2016 - Forum software by © MyBB