Skip to content

Instantly share code, notes, and snippets.

@hemache
Created November 24, 2012 12:10
Show Gist options
  • Select an option

  • Save hemache/4139417 to your computer and use it in GitHub Desktop.

Select an option

Save hemache/4139417 to your computer and use it in GitHub Desktop.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
define('DS', DIRECTORY_SEPARATOR, TRUE);
class Template {
public $template_dir;
public $template_cache;
public $template_expiry;
public $template_extension;
public function __construct()
{
// codeigniter instance
$this->ci =& get_instance();
// load setting model
$this->ci->load->model('setting');
// codeigniter constent (look ~/index.php)
$this->template_theme = $this->ci->setting->get('site_theme');
$this->template_dir = APPPATH.DS.'views'.DS.$this->template_theme.DS;
$this->template_cache = APPPATH.DS.'cache'.DS;
$this->template_expiry = 3600*24; // default to 24 hours
$this->template_extension = 'tpl';
}
public function load($file){
$template = new templateHandler($file, $this);
return $template;
}
}
class TemplateHandler {
public $file;
public $content;
public $output;
public $app;
public $src,$cachefile,$cachecontent,$time;
public function __construct($file,$app){
$this->file = $file;
$this->app = $app;
$this->load();
// codeigniter instance
$this->ci =& get_instance();
}
public function load(){
$src = $this->app->template_dir.$this->file.".".$this->app->template_extension;
$this->cachefile = $this->app->template_cache . md5($this->file) .".".$this->app->template_extension;
$inc = file_exists($src);
if(!$inc)
{
// use codeigniter logging system instead
$msg = sprintf("Template file [%s] doesn't exist", $this->file);
log_message('debug', $msg);
}
else
{
$source = fopen($src,'r');
$file = @fread($source,filesize($src));
fclose($source);
$cache = fopen($this->cachefile,'w+');
$this->cachecontent = @fread($cache,filesize($this->cachefile));
fclose($cache);
$this->content = $file;
$this->cachecontent = $file;
$this->output = $file;
$this->output = preg_replace("/\{(.*)\}/siU","<?php echo \\1; ?>",$this->output);
$this->output = preg_replace_callback("/\<(for|while|do|if|elseif|foreach)(\s+)\((.+)\)(\s*)\>/siU",array($this,"parse_cond_template"),$this->output);
$this->output = str_replace("</end>","<?php } ?>",$this->output);
}
$this->should_update = ((file_exists($this->cachefile) && time() - (($this->app->template_expiry * 60 * 60) >= filemtime($this->cachefile)) && $this->output!=$this->cachecontent) || !file_exists($this->cachefile));
$this->time = file_exists($this->cachefile) && !$this->should_update?filemtime($this->cachefile):time();
}
public function assign($var,$content){
$this->vars[$var] = $content;
$this->output = str_replace($var,"\$this->vars[\"$var\"]",$this->output);
if(is_array($content)){
$this->assign_array($var,$content);
}
}
private function get_tree($tree){
if($tree == "") return "";
$explode = explode(".",$tree);
$return = "";
foreach($explode as $key){
if(preg_match("/^[a-zA-Z0-9_]+$/siU",$key)){
$return .= "[\"$key\"]";
}elseif($key!=""){
console::log("Invalid array key used in Template::assign \"$key\"","TEMPLATE");
}
}
return $return;
}
public function assign_array($var,$array,$tree=""){
foreach($array as $key=>$value){
$current = $tree.".".$key;
$this->output = str_replace($var.$current,"\$this->vars[\"$var\"]".$this->get_tree($current)."",$this->output);
if(is_array($value)) $this->assign_array($var,$value,$current);
}
}
public function save(){
if($this->should_update){
$source = fopen($this->cachefile,'w+');
fwrite($source,$this->output);
fclose($source);
}
}
public function get_template($url){
$template = new TemplateHandler($url,$this->app);
foreach($this->vars as $k=>$v){
$template->assign($k,$v);
}
return $template->render(true);
}
public function getVariable($key){
return $this->vars[$key];
}
private function parse_include_template($input){
eval("\$return = \$this->get_template({$input[1]});");
return $return;
}
private function parse_cond_template($input){
return "<?php ".$input[1]."(".$input[3]."){ ?>";
}
private function parse_template(){
$this->output = preg_replace_callback("/\<include\s+file=\"(.+)\"\s*\/\>/siU",array($this,"parse_include_template"),$this->output);
$this->output = preg_replace("/lang_([a-zA-Z0-9_]+)/","lang(\"\\1\")",$this->output);
}
public function render($return=FALSE){
$this->parse_template();
$template = $this;
$this->save();
if($return)
{
return $this->output;
}
else
{
ob_start();
include_once($this->cachefile);
$output = ob_get_clean();
// append to global output
$this->ci->output->append_output($output);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment