Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Template
- {
- public $vars;
- public $tpldirs;
- public $xhtml = true;
- public $wrapper = "wrapper.php";
- public $charset = "utf-8";
- public function __construct()
- {
- $this->var = &$this->vars;
- }
- public function set($var,$val)
- {
- if(is_array($var))
- {
- foreach($var as $k => $v)
- { $this->vars[$var] = $val[$k]; }
- }
- else
- {
- $this->vars[$var] = $val;
- }
- }
- public function get($var)
- {
- return $this->vars[$var];
- }
- public function fetch($file)
- {
- if(is_array($this->vars)) { foreach($this->vars as $var => $val) { ${$var} = $val; } }
- ob_start();
- $found = false;
- if(!is_array($this->tpldir)) { $this->tpldir = array($this->tpldir); }
- foreach($this->tpldir as $dir)
- {
- if(substr($dir,strlen($dir)-1,1) != "/") { $dir .= "/"; }
- if(is_file($dir.$file)) { require_once($dir.$file);$found = true; }
- }
- if(!$found) { ob_end_clean();return false; }
- $content = ob_get_contents();
- ob_end_clean();
- return $content;
- }
- public function display($page,$content_type = null)
- {
- if($content_type == null && $this->xhtml)
- {
- if(strpos($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") !== FALSE || $_SERVER["HTTP_ACCEPT"] == "")
- {
- $content_type = "application/xhtml+xml;charset=utf-8";
- } else {
- $content_type = "text/html;charset=utf-8";
- }
- }
- list($this->mime,$charset) = explode(";",$content_type,2);
- list(,$this->charset) = explode("=",$charset,2);
- $content = $this->fetch($page);
- if(!$content) { $content = $this->fetch($page.".php"); }
- if(!$content) { $this->content = $page; } else { $this->content = $content; }
- header("Content-Type: {$content_type}");
- echo($this->fetch($this->wrapper));
- }
- public function is_ajax()
- {
- if
- (
- (isset($_GET["_ajax"]) && intval($_GET["_ajax"]))
- ||
- (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'))
- )
- { return true; }
- else
- { return false; }
- }
- public function ajax_print($array,$die = false)
- {
- header("Cache-Control: no-cache, must-revalidate");
- header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
- header("Content-Type: application/json");
- if(isset($_REQUEST["callback"]))
- {
- header("Content-Type: text/javascript;charset=utf-8");
- echo($_REQUEST["callback"]."(");
- }
- echo(json_encode($data));
- if(isset($_REQUEST["callback"]))
- {
- echo(");");
- }
- if($die) { die(); }
- return true;
- }
- }
Add Comment
Please, Sign In to add comment