Created
October 10, 2011 03:00
-
-
Save jamesfinley/1274546 to your computer and use it in GitHub Desktop.
Simple template controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$template = array(); | |
$template['routes'] = array( | |
'(?P<slug>[a-z0-9\-])' => 'view', | |
'(?P<slug>[a-z0-9\-])/comment' => 'create_comment', | |
'default_action' => 'index' | |
); | |
class Blog_Template extends Starter_Controller { | |
function index() | |
{ | |
$page = $this->input->get('page') ? $this->input->get('page') : 1; | |
$this->_template('blog/index', array( | |
'articles' => $this->article_model->find(array('page' => $page)) | |
)); | |
} | |
function view($vars = null) | |
{ | |
$slug = $vars['slug']; | |
$this->_template('blog/view', array( | |
'article' => $this->article_model->first(array('slug' => $slug)) | |
)); | |
} | |
function create_comment($vars = null) | |
{ | |
$slug = $vars['slug']; | |
} | |
} | |
class Starter_Controller { | |
function __construct($page) { | |
//store instance of CI | |
$this->CI =& get_instance(); | |
//store page info | |
$this->page = $page; | |
//include module helpers and models | |
$includes = $page->includes(); | |
if (count($includes['helper'])) | |
{ | |
foreach ($includes['helper'] as $helper) | |
{ | |
$this->CI->load->helper(str_replace('.php', '', $helper)); | |
} | |
} | |
if (count($includes['model'])) | |
{ | |
foreach ($includes['model'] as $model) | |
{ | |
$this->CI->load->model(str_replace('.php', '', $model)); | |
} | |
} | |
//check routes | |
$this->routes = array('(?P<action>[a-z0-9\-])' => '$action'); | |
if (isset($this->page->routes) && $this->page->routes) | |
{ | |
$this->routes = $this->page->routes; | |
} | |
$this->_route(); | |
} | |
function _route() | |
{ | |
$uri = $this->CI->uri->uri_string(); | |
$slug = addslashes('/'.$this->page->full_slug); | |
$uri = preg_replace('/^'.$slug.'/', '', $uri); | |
$matched = null; | |
foreach ($this->routes as $route => $action) | |
{ | |
if ($route != 'default_action' && preg_match('/^'.$route.'/', $uri, $matches)) | |
{ | |
$matched = (object) array('route' => $route, 'action' => $action, 'info' => $matches); | |
} | |
} | |
if ($matched != null) | |
{ | |
$action = isset($matched->info['action']) ? $matched->info['action'] : $matched->action; | |
if (function_exists($this->$action)) | |
{ | |
$this->action($matched->info); | |
} | |
} | |
else | |
{ | |
$action = $this->routes['default_action']; | |
if (function_exists($this->$action)) | |
{ | |
$this->action(); | |
} | |
} | |
} | |
function _template($name, $data = array()) | |
{ | |
//extract variables | |
extract($page->variables()); | |
extract($data); | |
//load template | |
include('assets/site/templates/'.$name.'.php'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment