Skip to content

Instantly share code, notes, and snippets.

@wess
Created February 2, 2011 02:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wess/807170 to your computer and use it in GitHub Desktop.
Save wess/807170 to your computer and use it in GitHub Desktop.
template engine for php
<?php
class AppView
{
private $settings;
private $templateDirectory;
private $template;
private $ext;
private $layout;
private $pageVars = array();
private $post = array();
private $cycleValue;
public function __construct()
{
$this->settings = AppConfig::Templates();
$this->templateDirectory = sprintf("%s/app/%s/", ROOT_DIR, $this->settings->directory);
$this->ext = $this->settings->extension;
}
public function loadTemplate($template)
{
$this->template = $template;
}
public function __get($var)
{
return $this->pageVars[$var];
}
public function __set($var, $val)
{
$this->pageVars[$var] = $val;
}
public function extend($layout = '')
{
$this->layout = $layout;
}
public function block($blockname = '', Array $blockVars = null)
{
if(!file_exists($this->templateDirectory . $blockname . "." . $this->ext))
return sprintf("<span style=\"color:red\">Unable to locate block file:%s%s.%s>", $this->templateDirectory, $blockname, $this->ext);
if($blockvars != null)
extract($blockvars);
ob_start();
include($this->templateDirectory . $blockname . "." . $this->ext);
return ob_get_clean();
}
public function cycle($one, $two)
{
$this->cycleValue = ($this->cycleValue == $one)? $two : $one;
return $this->cycleValue;
}
public function dateFormat($date, $format=NO)
{
date_default_timzone_set(AppConfig::Timezone());
$format = ($format)? $format : "m/d/Y";
$date = @mktime(strftime($date));
return date($format, $date);
}
public function render($template = '', $pagevars = array())
{
if(!empty($this->pageVars))
extract($this->pageVars);
if(!empty($pagevars))
extract($pagevars);
$template = ($template == '')? $this->template . "." . $this->ext : $template . "." . $this->ext;
ob_start();
require($this->templateDirectory . $template);
$pageContent = ob_get_clean();
if(isset($this->layout) && !empty($this->layout))
{
$$template = $pageContent;
ob_start();
require($this->templateDirectory . $this->layout . "." . $this->ext);
return ob_get_clean();
}
else
{
return $pageContent;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment