I inherited this class from somewhere, and don't know who to attribute the original work to, but it was a very, very slim class before and was used in conjunction with another "DynamicLoad.php" class.
I got my hands on it and merged the two ideas making it a very useful tool for me. I extended it to handle assets (js/css/icons) as well as meta tags and yada yada. It's fairly robust, and should be simple enough to build on top of. Enjoy.
In my application/views/user/template.php file:
Code:
<html lang="<?php echo $this->template->renderLanguage() ?>">
<head>
<?php echo $this->template->renderHead() ?>
</head>
<body>
...
<?php echo $content ?>
...
<?php echo $this->template->renderFoot() ?>
</body>
</html>
Custom parent controller (setup some base template properties/assets):
Code:
class Frontend_Controller extends Website_Controller {
public function __construct() {
parent::__construct();
$this->template
->setBase('/')
->setCSS('//cdn.jsdelivr.net/foundation/5.5.0/css/foundation.min.css')
->setCSS('/assets/foundation-icons/foundation-icons.css')
->setCSS('/assets/css/frontend.css')
->setCSS('//fonts.googleapis.com/css?family=Nothing+You+Could+Do')
->setIcon('/favicon.ico', 'shortcut icon', 'image/ico')
->setIcon('/favicon.ico', 'apple-touch-icon', 'image/ico')
->setJS('//cdn.jsdelivr.net/foundation/5.5.0/js/vendor/modernizr.js', 'head')
->setJS('//cdn.jsdelivr.net/jquery/2.0.3/jquery-2.0.3.min.js', 'foot')
->setJS('//cdn.jsdelivr.net/jquery.hotkeys/0.8b/jquery.hotkeys.min.js', 'foot')
->setJs('//cdn.jsdelivr.net/foundation/5.5.0/js/foundation.min.js', 'foot')
->setJS('/assets/js/frontend.js', 'foot')
->setMeta('og:image', siteUrl('assets/social-logo.png'))
->setMeta('og:site_name', 'Website.com')
->setMeta('og:title', 'Home of the website users')
->setMeta('og:type', 'website')
->setMeta('og:url', siteUrl())
->setMeta('viewport', 'width=device-width,initial-scale=1.0')
->setSiteTitle('Website, home of the website users', ' - ')
->setTemplate('frontend/template');
}
}
Sample Controller (setup the view specific template stuff):
Code:
class Users extends Frontend_Controller {
...
public function profile($user_name) {
$user = new User();
$user->loadByUserName($user_name);
if(!$user->is_loaded) :
show_404();
endif;
$template_data = array(
'user' => $user,
);
$this->template
->setMeta('og:title', 'Profile for '.$user->meta->nickname_filtered)
->setMeta('og:image', siteUrl($user->avatar->meta->src))
->setMeta('og:url', $user->profile_url)
->setTitle('Profile for '.$user->meta->nickname_filtered)
->setTemplate('user/template')
->view('user/profile', $template_data);
}
...
}
Actual Template Class:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Template
*
* Enables the user to load template
*
* Usage:
*
*
* Configure CodeIgniter to Auto-Load it:
*
* Edit application/config/autoload.php
* $autoload['libraries'] = array('Template');
*
*
*/
class Template {
private $assets = array();
private $CI;
private $language;
private $meta = array();
private $site_title;
private $template = '';
private $title_delim = ' - ';
public function __construct() {
$this->CI =& get_instance();
# Set some defaults
$this->setBase('/');
$this->setCharset('utf-8');
$this->setLanguage('en');
}
public function buildCSS() {
$tags = array();
if(is_array($this->assets['css']) && !empty($this->assets['css'])) :
foreach($this->assets['css'] as $asset) :
$tags[] = '<link rel="stylesheet" type="text/css" href="'.$asset['href'].'">';
endforeach;
endif;
return $tags;
}
public function buildJS($positions, $positions_type='include') {
$positions = is_array($positions) ? array_map('trim', $positions) : array_map('trim', explode(',', $positions));
$tags = array();
if(is_array($this->assets['js']) && !empty($this->assets['js'])) :
foreach($this->assets['js'] as $asset) :
switch($positions_type) :
case 'include' :
if(!in_array($asset['position'], $positions)) :
continue 2;
endif;
break;
case 'exclude' :
if(in_array($asset['position'], $positions)) :
continue 2;
endif;
break;
default :
continue 2;
break;
endswitch;
$tags[] = '<script type="text/javascript" src="'.$asset['src'].'"></script>';
endforeach;
endif;
return $tags;
}
public function buildIcons() {
$tags = array();
if(is_array($this->assets['icons']) && !empty($this->assets['icons'])) :
foreach($this->assets['icons'] as $asset) :
$tags[] = '<link rel="'.$asset['rel'].'" type="'.$asset['type'].'" href="'.$asset['href'].'">';
endforeach;
endif;
return $tags;
}
public function buildMeta() {
$tags = array();
# Prepare title
if(empty($this->meta['title'])) :
$this->meta['title'] = $this->site_title;
elseif(empty($this->site_title)) :
$this->meta['title'] = $this->meta['title'];
else :
$this->meta['title'] = $this->meta['title'].$this->title_delim.$this->site_title;
endif;
# Output charset first
$tags[] = '<meta charset="'.$this->meta['charset'].'">';
unset($this->meta['charset']);
if(is_array($this->meta) && !empty($this->meta)) :
foreach($this->meta as $name => $content) :
switch(strtolower($name)) :
case 'base' :
if(!empty($content)) :
$tags[] = '<base href="'.$content.'">';
endif;
break;
case 'title' :
$tags[] = '<title>'.$content.'</title>';
break;
default :
$tags[] = '<meta name="'.$name.'" content="'.$content.'">';
break;
endswitch;
endforeach;
endif;
return $tags;
}
public function renderFoot($return=FALSE) {
$output = array_merge(
$this->buildJS('head', 'exclude')
);
$output = implode("\n\t\t", $output)."\n";
if(is_bool($return) && $return === TRUE) :
return $output;
endif;
echo $output;
return TRUE;
}
public function renderHead($return=FALSE) {
$output = array_merge(
$this->buildMeta(),
$this->buildIcons(),
$this->buildCSS(),
$this->buildJS('head', 'include')
);
$output = implode("\n\t\t", $output)."\n";
if(is_bool($return) && $return === TRUE) :
return $output;
endif;
echo $output;
return TRUE;
}
public function renderLanguage($return=FALSE) {
if(empty($this->language)) :
$output = $this->default_language;
else :
$output = str_replace(' ', '-', strtolower(trim($this->language)));
endif;
if(is_bool($return) && $return === TRUE) :
return $output;
endif;
echo $output;
return TRUE;
}
public function setBase($location) {
return $this->setMeta('base', $location);
}
public function setCharset($content) {
return $this->setMeta('charset', strtolower($content));
}
public function setCSS($href) {
if(!isset($this->assets['css'])) :
$this->assets['css'] = array();
endif;
$this->assets['css'][$href] = array(
'href' => $href,
);
return $this;
}
public function setIcon($href, $rel='icon', $type='image/ico') {
if(!isset($this->assets['icons'])) :
$this->assets['icons'] = array();
endif;
$this->assets['icons'][$href.$rel.$type] = array(
'href' => $href,
'rel' => $rel,
'type' => $type,
);
return $this;
}
public function setJS($src, $position='head') {
if(!isset($this->assets['js'])) :
$this->assets['js'] = array();
endif;
$this->assets['js'][$src] = array(
'src' => $src,
'position' => $position,
);
return $this;
}
public function setLanguage($language) {
$this->language = $language;
return $this;
}
public function setMeta($name, $content) {
$this->meta[$name] = $content;
return $this;
}
public function setSiteTitle($site_title='', $title_delim=' - ') {
$this->site_title = $site_title;
$this->title_delim = $title_delim;
return $this;
}
public function setTemplate($template) {
$this->template = $template;
return $this;
}
public function setTitle($content='') {
return $this->setMeta('title', $content);
}
public function view($view, $data=NULL, $return=FALSE) {
$data = !is_array($data) ? array() : $data;
$data['content'] = $this->CI->load->view($view, $data, TRUE);
if(is_bool($return) && $return === TRUE) :
if(!empty($this->template)) :
return $this->CI->load->view($this->template, $data, TRUE);
endif;
return $data['content'];
endif;
if($this->CI->input->is_ajax_request()) :
$this->setTemplate('');
endif;
if(!empty($this->template)) :
$this->CI->load->view($this->template, $data, FALSE);
else :
echo $data['content'];
endif;
return TRUE;
}
public function viewPartial($view, $data=NULL, $return=FALSE) {
$current_template = $this->template;
$result = $this->setTemplate(NULL)->view($view, $data, $return);
$this->setTemplate($current_template);
return $result;
}
}
/* End of file Template.php */
/* Location: ./application/libraries/Template.php */