<?
/*
class name: nGenie
description: simple template engine
author: Sergey Krivoy <hide@address.com>
version: 0.1
*/
class nGenie {
var $section;
var $data;
var $body_tpl, $iface_tpl;
var $tpl_path;
function nGenie($tpl_path = "") {
if ($tpl_path != "") {
$this->tpl_path = $tpl_path."/";
} else {
$this->tpl_path = "";
}
$this->data["DOCUMENT_URL"] = "http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
function start($body_tpl, $iface_tpl = NULL, $section = NULL) {
if (is_readable($this->tpl_path.$body_tpl)) {
$this->body_tpl = join("", file($this->tpl_path.$body_tpl));
} else {
echo "body template '$body_tpl' not exists";
}
if (isset($iface_tpl)) {
if (is_readable($this->tpl_path.$iface_tpl)) {
$this->iface_tpl = join("", file($this->tpl_path.$iface_tpl));
} else {
echo "interface '$iface_tpl' not exists";
}
}
if (isset($section)) {
$this->section = $section;
}
}
function set_param($name, $value) {
$this->data[$name] = $value;
}
function set_block($name, $block) {
$this->data[$name][] = $block;
}
function output() {
$body = $this->parse_body();
if (isset($this->iface_tpl)) {
$iface = $this->parse_iface();
$body = preg_replace("/\{BODY\}/", $body, $iface);
}
//$body = $this->prepare($body);
echo $body;
}
function parse_body() {
$body = $this->body_tpl;
if (count($this->data)) foreach ($this->data as $key => $value) {
if (is_array($value)) {
$blocks = array();
foreach ($value as $block) {
if (preg_match("/<!-- BEGIN ".$block->name." -->(.*)<!-- END ".$block->name." -->/s", $body, $matches)) {
$blocks[] = $block->parse($matches[1]);
}
}
if (count($blocks)) {
$body = preg_replace("/\{(".$key.")\}/", join("", $blocks), $body);
}
} else {
$body = preg_replace("/\{(".$key.")\}/", $value, $body);
}
}
$body = preg_replace("/<!-- BEGIN (\w+) -->(.*)<!-- END \\1 -->/s", "", $body);
return $body;
}
function parse_iface() {
$iface = $this->iface_tpl;
if (count($this->data)) foreach ($this->data as $key => $value) {
if (!is_array($value)) {
$iface = preg_replace("/\{(".$key.")\}/", $value, $iface);
}
}
if (count($this->section) and preg_match_all("/<!-- MENUBEGIN (\w+)_(active|passive) -->(.*)<!-- MENUEND \\1_\\2 -->/s", $iface, $matches)) {
foreach ($matches[1] as $key => $value) if (in_array($value, $this->section) and ($matches[2][$key] == "active")) {
$menu_items[] = $matches[3][$key];
} else if (!in_array($value, $this->section) and $matches[2][$key] == "passive") {
$menu_items[] = $matches[3][$key];
}
$iface = preg_replace("/\{MENU\}/", join("", $menu_items), $iface);
$iface = preg_replace("/<!-- MENUBEGIN (\w+)_(active|passive) -->.*<!-- MENUEND (\w+)_(active|passive) -->/s", "", $iface);
} else {
echo "empty $this->section or not matched";
}
return $iface;
}
function make($text) {
$text = preg_replace("/(\r|\n)/s", "", $text);
$text = preg_replace("/([\s]{2,})/s", " ", $text);
return $text;
}
function prepare($text) {
$text = preg_replace("/(\r|\n)/s", "", $text);
$text = preg_replace("/([\s]{2,})/s", " ", $text);
return $text;
}
}
class block {
var $name;
var $data;
function block($name, $data = NULL) {
$this->name = $name;
$this->data = $data;
}
function set_param($name, $value) {
$this->data[$name] = $value;
}
function set_block($name, $block) {
$this->data[$name][] = $block;
}
function parse($template) {
if (count($this->data)) foreach ($this->data as $key => $value) {
if (is_array($value)) {
$blocks = array();
foreach ($value as $block) {
if (preg_match("/<!-- BEGIN ".$block->name." -->(.*)<!-- END ".$block->name." -->/s", $template, $matches)) {
$blocks[] = $block->parse($matches[1]);
} else {
echo $block->name." not found in <pre>".$template."</pre>";
}
}
$template = preg_replace("/\{(".$key.")\}/", join("", $blocks), $template);
$template = preg_replace("/<!-- BEGIN (\w+) -->(.*)<!-- END \\1 -->/s", "", $template);
} else {
$template = preg_replace("/\{(".$key.")\}/", $value, $template);
}
}
return $template;
}
}
?>