Skip to content

Instantly share code, notes, and snippets.

@conord
Created July 14, 2012 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conord/3112974 to your computer and use it in GitHub Desktop.
Save conord/3112974 to your computer and use it in GitHub Desktop.
Template library for Codeigniter
<?php
/**
*
*/
class Template
{
/**
* @var array
*/
private $javascript_src = array();
/**
* @var array
*/
private $javascript_code = array();
/**
* @var array
*/
private $css_src = array();
/**
* @var array
*/
private $css_code = array();
/**
* @var
*/
private $rendered_html;
/**
* @var
*/
private $menu;
/**
*
*/
public function __construct()
{
$this->CI =& get_instance();
$this->load_template();
}
/**
* @param $src
*/
public function set_javascript_src($src)
{
if (true === is_array($src))
{
foreach ($src as $link)
{
$this->javascript_src[] = $link;
}
}
else
{
$this->javascript_src[] = $src;
}
}
/**
* @param $code
*/
public function set_javascript_code($code)
{
if (true === is_array($code))
{
foreach ($code as $js)
{
$this->javascript_code[] = $js;
}
}
else
{
$this->javascript_code[] = $code;
}
}
/**
* @param $css
*/
public function set_css_src($css)
{
if (true === is_array($css))
{
foreach ($css as $style)
{
$this->css_src[] = $style;
}
}
else
{
$this->css_src[] = $css;
}
}
/**
* @param $css
*/
public function set_css_code($css)
{
if (true === is_array($css))
{
foreach ($css as $style)
{
$this->css_code[] = $style;
}
}
else
{
$this->css_code[] = $css;
}
}
/**
* @param $menu
*/
public function set_menu($menu)
{
$this->menu = $menu;
}
/**
* Load the header/footer for the template based the store.
* Do not render the view
*
*/
public function load_template()
{
$seg = $this->CI->uri->segment(1);
if (strtolower($seg) == 'admin')
{
$this->header = "templates/admin/header";
$this->footer = "templates/admin/footer";
}
else
{
$this->header = "templates/header";
$this->footer = "templates/footer";
}
}
/**
*
*/
public function render()
{
$seg = $this->CI->uri->segment(1);
// build the page.
$header_data = array(
'css_code' => $this->css_code,
'css_src' => $this->css_src,
'javascript_code' => $this->javascript_code,
'javascript_src' => $this->javascript_src,
'menu' => $this->CI->load->view("templates/". $this->menu,'',true)
);
$footer_data = "";
$header_html = $this->CI->load->view($this->header,$header_data,true);
$footer_html = $this->CI->load->view($this->footer,$footer_data,true);
// set the data
$data['template'] = $header_html . $this->rendered_html. $footer_html;
// load the page
$this->CI->load->view('template',$data);
}
/**
* @param $view
* @param $data
*/
public function load_view($view,$data)
{
// determine what to load. Look for site-specific code first, then base
$this->rendered_html .= $this->CI->load->view($view,$data,true);
$this->render();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment