Jump to content

Setting Up My Site With Code Igniter Template


unemployment

Recommended Posts

I’m very new to CF and MVC in general, but I have been writing php for about a year.

 

I was working through a NetTutsPlus tutorial on how to setup a template system and I already hit some road blocks that I have no idea how to solve. I want to use a template because I want to be as efficient as possible.  If it helps, here is a link to the tutorial I worked from

 

1.  I don’t know how to change the URL structure.

Currently the template system forces me to go to index.php/site/blog for example.

 

2.  I don’t know how to pass in dynamic data to my header and footer using this system.

 

3. What does extends do?  Example: “class Site extends CI_Controller”

 

Here is my current setup…

 

views/template.php

<?php

$this->load->view('/includes/header');

$this->load->view($main_content);

$this->load->view('/includes/footer');

?> 

 

controllers/site.php

<?php

class Site extends CI_Controller{

function header()
{
  $this->load->library('common');
  $page_names = $this->common->pages();
  
  $page = substr(end(explode(DIRECTORY_SEPARATOR, $_SERVER['PHP_SELF'])), 0, -4);
  
  $title = (array_key_exists($page, $page_names) !== false) ? $page_names[$page]: '';
  
  if (array_key_exists($page, $page_names) !== false)
  {
   $title .= " | Jason Biondo";
  }
  
  $data['header_content'] = $title;
  $this->load->view('template', $data);
}

function about_us()
{
  $data['main_content'] = 'about_us';
  $this->load->view('template', $data);
}

function blog()
{
  $data['main_content'] = 'blog';
  $this->load->view('template', $data);
}
}

?>

 

views/includes/header.php

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" dir="ltr">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="content-script-type" content="text/javascript" />
<title><?php echo $title; ?></title>
</head>
<body>
<div id="container">  

1. For the url structure, I recommend solution 3 on the following link:

 

http://codeigniter.com/wiki/Dreamhost_.htaccess

 

2.  You are passing dynamic data, you are just looking for it wrong.

 

example, you have

 

<?php
$data['header_content'] = $title;
  $this->load->view('template', $data); ?>

 

In order to get this in your view, you would need to call it like this:

 

<title><?php echo $header_content; ?></title>

 

3. Extends means:  you are able to use core codeigniter functions

You wouldn't be able to use:

<?php
$this->load->view(); ?>

If you do not 'EXTENDS' the core functionality.

  • 4 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.