<?php
class Page
{
var $page;
function Page($template = "indexbody.htm") {
if (file_exists($template))
$this->page = join("", file($template));
else
die("Template file $template not found.");
}
function parse($file) {
ob_start();
include($file);
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
function replace_tags($tags = array()) {
if (sizeof($tags) > 0)
foreach ($tags as $tag => $data) {
$data = (file_exists($data)) ? $this->parse($data) : $data;
$this->page = eregi_replace("{" . $tag . "}", $data,
$this->page);
}
else
die("No tags designated for replacement.");
}
function output() {
echo $this->page;
}
}
?>
require_once("template.php");
$page = new Page("body.htm");
$page->replace_tags(array(
"tag1" => "filnavn",
"tag2"=> "filnavn"
));
$page->output();
require_once("template.php");
$page = new Page("body.htm");
$page->set_filename(array(
"tag1" => "filnavn",
"tag2"=> "filnavn"
));
$page->assign_block_vars('test', array(
'TEXT' => "HELLO"
'TEXT1' => "HELLO1"
));
$page->output();
<?php
/**
* A simple and low footprint templating engine
* refer to @link for full documentation
*
* @copyright 2008 pat ambrosio
* @license http://www.opensource.org/licenses/mit-license.php
* @version Release: @0.2.3@
* @link http://code.google.com/p/mars-templater/
* @since Class available since Release 0.1
*
* @Modified 10-2009 R. Stormo
* @version Release: @0.2.6.5@
*/
class templater
{
var $filename;
var $filestring;
var $vars = array();
var $loops = array();
var $cons = array();
var $files = array();
function templater($filename='')
{
$this->filename = $filename;
$this->filestring = ($filename != '') ? file_get_contents($this->filename) : '';
}
function setfile($filename)
{
$this->templater($filename);
}
function setfilestring($filestring)
{
$this->filestring = $filestring;
}
function addvar($varname, $data)
{
$this->vars[$varname] = $data;
}
function addfile($varname, $data)
{
$data = (file_exists($data)) ? $this->parse($data) : $data;
$this->files[$varname] = $data;
}
function addloop($varname, $data)
{
$this->loops[$varname] = $data;
}
function addcon($varname, $data)
{
$this->cons[$varname] = $data;
}
function setvars($data)
{
$this->vars = $data;
}
function setloops($data)
{
$this->loops = $data;
}
function setcons($data)
{
$this->loops = $data;
}
function render($rules='parse all remove all')
{
$newfile = ' '.$this->filestring;
$rules = str_replace('all', 'files cons vars loops', $rules);
$steps = explode(' ', $rules);
$w = 'parse';
foreach($steps as $step)
{
$w = (strcmp($step, 'parse')===0) ? 'parse' : $w ;
$w = (strcmp($step, 'remove')===0) ? 'remove' : $w ;
switch($step)
{
case 'files':
switch($w)
{
case 'parse':
$newfile = (!empty($this->vars)) ? $this->parsefiles($newfile) : $newfile;
break;
case 'remove':
//$newfile = $this->removefiles($newfile);
$newfile = $newfile;
break;
}
break;
case 'cons':
switch($w)
{
case 'parse':
$newfile = (!empty($this->cons)) ? $this->parsecons($newfile) : $newfile;
break;
case 'remove':
$newfile = $this->removecons($newfile);
break;
}
break;
case 'vars':
switch($w)
{
case 'parse':
$newfile = (!empty($this->vars)) ? $this->parsevars($newfile) : $newfile;
break;
case 'remove':
$newfile = $this->removevars($newfile);
break;
}
break;
case 'loops':
switch($w)
{
case 'parse':
$newfile = (!empty($this->loops)) ? $this->parseloops($newfile) : $newfile;
break;
case 'remove':
$newfile = $this->removeloops($newfile);
break;
}
break;
case 'none' : ; case '' : ; default : ; break;
}
}
return ltrim($newfile, ' ');
}
function output() {
echo $this->render();
}
//--------------------
function parse($file) {
ob_start();
include($file);
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
//-------------------
/*
THE FOLLOWING CAN CAUSE YOU MUCH CONFUSION, MIND ME.
*/
function removecons($filestring)
{
$filecons = $filestring;
$pos = -1;
while($pos!==false){
$pos = strpos($filecons, '{con:', $pos+1);
if($pos !== false)
{
$tagend = strpos($filecons, '}', $pos);
$key = substr($filecons, $pos+5, $tagend-$pos-5); //parse Key from found con
$tplconend = strpos($filecons, '{/con:'.$key.'}', $tagend ); //end of the text to be con
$endtaglength = strlen('{/con:'.$key.'}');
$filecons = substr_replace($filecons, '', $pos, $tplconend+$endtaglength-$pos);
}
}
return $filecons;
}
function removeloops($filestring)
{
$fileloops = $filestring;
$pos = -1;
while($pos!==false){
$pos = strpos($fileloops, '{loop:', $pos+1);
if($pos !== false)
{
$tagend = strpos($fileloops, '}', $pos);
$key = substr($fileloops, $pos+6, $tagend-$pos-6); //parse Key from found loop
$tplloopend = strpos($fileloops, '{/loop:'.$key.'}', $tagend ); //end of the text to be loop
$endtaglength = strlen('{/loop:'.$key.'}');
$fileloops = substr_replace($fileloops, '', $pos, $tplloopend+$endtaglength-$pos);
}
}
return $fileloops;
}
function removevars($filestring)
{
$filevars = $filestring;
$pos = -1;
while($pos!==false){
$pos = strpos($filevars, '{', $pos+1);
if($pos !== false)
{
$tagend = strpos($filevars, '}', $pos);
$var = substr($filevars, $pos+1, $tagend-$pos-1); //parse Key from found var
//echo $var,'<br />';
if(strpos($var, ' ', 0)===false && strpos($var, "\n", 0)===false && strlen($var)>0)
{
if(strpos($var, 'con:', 0)!==false || strpos($var, 'loop:', 0)!==false )
{
if(strpos($var, 'con:', 0)!==false)
{
$key = substr($filevars, $pos+5, $tagend-$pos-5);
$tplconend = strpos($filevars, '{/con:'.$key.'}', $tagend );
$pos = $tplconend + strlen('{/con:'.$key.'}');
}
if(strpos($var, 'loop:', 0)!==false)
{
$key = substr($filevars, $pos+6, $tagend-$pos-6);
$tplloopend = strpos($filevars, '{/loop:'.$key.'}', $tagend );
$pos = $tplloopend + strlen('{/loop:'.$key.'}');
}
}else{
$filevars = substr_replace($filevars, '', $pos, $tagend-$pos+1);
//echo $pos,' hey ',$tagend-$pos;
//echo substr($filevars, $pos, $tagend-$pos+1);
}
//*/
}
}
}
return $filevars;
}
//-------------------------------------------------------------------------------------------------//
function parsecons($filestring)
{
$filecons = $filestring;
$conkeys = array_keys($this->cons);
$pos = -1;
while($pos!==false){
$pos = strpos($filecons, '{con:', $pos+1);
if($pos !== false)
{
$tagend = strpos($filecons, '}', $pos);
$key = substr($filecons, $pos+5, $tagend-$pos-5); //parse Key from found con
$tplconend = strpos($filecons, '{/con:'.$key.'}', $tagend ); //end of the text to be con
$endtaglength = strlen('{/con:'.$key.'}');
if($this->cons[$key]===false)
{
//erase conditional block
$filecons = substr_replace($filecons, '', $pos, $tplconend+$endtaglength-$pos);
}else if($this->cons[$key]===true){
//erase tags
$filecons = substr_replace($filecons, '', $tplconend, $endtaglength);
$filecons = substr_replace($filecons, '', $pos, $tagend-$pos+1);
}
}
}
return $filecons;
}
function parsevars($filestring)
{
$keys = array_keys($this->vars);
$newkeys = array();
foreach($keys as $key)
$newkeys[] = '{'.$key.'}';
$taggifiedarray = array_combine($newkeys, array_values($this->vars));
$filevars = str_replace(array_keys($taggifiedarray),
array_values($taggifiedarray),
$filestring);
return $filevars;
}
function parseloops($filestring)
{
$fileloops = $filestring;
$loopkeys = array_keys($this->loops);
$pos = -1;
while($pos!==false){
$pos = strpos($fileloops, '{loop:', $pos+1);
if($pos !== false)
{
$tagend = strpos($fileloops, '}', $pos);
$key = substr($fileloops, $pos+6, $tagend-$pos-6); //parse Key from found loop
$tplloopstart = $tagend+1; //start of the text to be looped
$tplloopend = strpos($fileloops, '{/loop:'.$key.'}', $tagend ); //end of the text to be looped
$tplloop = substr($fileloops, $tplloopstart, $tplloopend-$tplloopstart); //the text to be looped
$parse = '';
if(!empty($this->loops[$key])){ //do below if key exists in array loops
$tags = array();
foreach($this->loops[$key] as $row){
$keys = array_keys($row);
foreach($keys as $k){
$tags[] = '{'.$k.'}';
}
$tags = array_unique($tags);
$parse .= str_replace($tags, array_values($row), $tplloop);
}
$fileloops = substr_replace($fileloops, $parse, $pos, $tplloopend+strlen('{/loop:'.$key.'}')-$pos);
}
}
}
return $fileloops;
}
//##########################
function parsefiles($filestring)
{
$keys = array_keys($this->files);
$newkeys = array();
foreach($keys as $key)
$newkeys[] = '{'.$key.'}';
$taggifiedarray = array_combine($newkeys, array_values($this->files));
$filevars = str_replace(array_keys($taggifiedarray),
array_values($taggifiedarray),
$filestring);
return $filevars;
}
}
?>
<?php
require_once("templater.class.php");
$page = new templater("mytemplate.htm");
$page->addvar('description','Album oversikt');
$page->addfile('body', 'htm/mybody.htm');
$page->addloop('bilder', $bildearray);
$page->output();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="nb">
<head>
</head>
<body>
{body}
</body>
</html>
{description}
{loop:bilder}
{bilde}
{navn}
{/loop:bilder}
Registrerte brukere: Google [Bot]