Created
July 29, 2012 03:05
-
-
Save ericlbarnes/3195886 to your computer and use it in GitHub Desktop.
CodeIgniter Template
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 if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Template { | |
/** | |
* CI Object | |
* | |
* @access protected | |
* @var object | |
*/ | |
protected $_ci = null; | |
/** | |
* Constructor | |
* | |
* @uses get_settings | |
* @return void | |
*/ | |
function __construct() | |
{ | |
$this->_ci =& get_instance();; | |
} | |
// ------------------------------------------------------------------------ | |
/** | |
* Test if a template file exists | |
* | |
* @access public | |
* @param string | |
* @return bool | |
*/ | |
protected function test_exists($file) | |
{ | |
$file = 'themes/'.$file; | |
if ( ! file_exists($file)) | |
{ | |
return false; | |
} | |
return true; | |
} | |
// ------------------------------------------------------------------------ | |
/** | |
* Load Body Template | |
* | |
* This method is used to load the body template file. Can be used without layout | |
* for emails, printer, etc... | |
* | |
* @access protected | |
* @param string the template body file. | |
* @param string the directory - admin or front. | |
* @param array data array | |
* @uses test_exists | |
* @return object I think | |
*/ | |
protected function load_body($template, $dir='front', $data) | |
{ | |
$body_file = $dir.'/'.$data['settings']['template'].'/'.$template.'.php'; | |
if ($this->test_exists($body_file)) | |
{ | |
return $this->_ci->load->view($body_file, $data, true); | |
} | |
else | |
{ | |
return $this->_ci->load->view($dir.'/default/'.$template, $data, true); | |
} | |
} | |
// ------------------------------------------------------------------------ | |
/** | |
* Load layout Template | |
* | |
* This method is used to load the layout template file. | |
* | |
* @access protected | |
* @param string the directory - admin or front. | |
* @param array data array | |
* @uses test_exists | |
* @return object | |
*/ | |
protected function load_layout($dir='front', $data) | |
{ | |
$layout_file = $dir.'/'.$data['settings']['template'].'/layout.php'; | |
if ($this->test_exists($layout_file)) | |
{ | |
return $this->_ci->load->view($layout_file, $data); | |
} | |
else | |
{ | |
return $this->_ci->load->view($dir.'/default/layout.php',$data); | |
} | |
} | |
// ------------------------------------------------------------------------ | |
/** | |
* Template Display | |
* | |
* Displays a template based off the current settings | |
* Basically a wrapper for layout | |
* | |
* <code> | |
* $this->init_model->display('category', $data); | |
* </code> | |
* | |
* @access public | |
* @param string the template body file | |
* @param array the data to pass | |
* @param string The directory. front or admin (Defaults to front) | |
* @uses get_setting | |
* @uses load_body | |
* @uses test_exists | |
*/ | |
public function display($template, $data='', $dir='front') | |
{ | |
// meta content | |
if ( ! isset($data['title'])) | |
{ | |
$data['title'] = $this->get_setting('site_name'); | |
} | |
if ( ! isset($data['meta_keywords'])) | |
{ | |
$data['meta_keywords'] = $this->get_setting('site_keywords'); | |
} | |
if ( ! isset($data['meta_description'])) | |
{ | |
$data['meta_description'] = $this->get_setting('site_description'); | |
} | |
// Check the body exists | |
$data['body'] = $this->load_body($template, $dir, $data); | |
// Now check the layout exists | |
$this->load_layout($dir, $data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment