<?php
class Template
{
private $DOMDoc;
public $vars;
public $templateDir;
public $NS="http://someexaple.com";
private $loops;
public function __construct($templateFile=null,$version=null,$encoding=null)
{
$this->DOMDoc = new DOMDocument();
if($templateFile)
$this->setTemplate($templateFile);
}
public function setTemplateDir($templateDir)
{
$this->templateDir = $templateDir;
}
public function setTemplate($templateFile)
{
$this->DOMDoc->load($this->templateDir.$templateFile);
return true;
}
private function select($path=null,$default=null)
{
switch($type)
{
case "@":
//petle
break;
case "#":
//dokument->xpath
break;
case "/":
$path =preg_replace("#/(w+)#is","['$1']",$path);//tworzymy sciezke do zmiennej $path="$input = $this->vars".$path.";";
break;
default:
if($default=="/end/")
$input = $path;
break;
}
return $input;
elseif($default)
return $this->select($default,'/end/');
}
private function replaceNodeWithString($oldNode,$xmlString)
{
$f = $this->DOMDoc->createDocumentFragment();
if($f->appendXML($xmlString.' '))//szybki hak na puste stringi :)
$oldNode->parentNode->replaceChild($f,$oldNode);
}
/*w przyszlosci doda podmienianie wsztsrkich tagow odnoszacych sie do tych samych
plikow*/
private function applyImports()
{
$e = $this->DOMDoc->getElementsByTagNameNS($this->NS, "import");
$i=0;
foreach($e as $item)
{
$includePath="";
if($item->hasAttribute("href"))
$includePath = $item->getAttribute("href");
elseif($item->hasAttribute("select"))
$includePath = $this->select($item->getAttribute("select"));
else
throw new Exception();
if($includePath)
{
//echo $includePath;
{
$toProgress[$i]['item']=$item;
$toProgress[$i]['data']= '<includedTPL xmlns:tpl="'.$this->NS.'">'.$data.'</includedTPL>';//tag ktory dodaje ns dla includa, ktory go nie ma
}
else
throw new Exception();
}
else
{
throw new Exception();
}
$i++;
}
if($toProgress){
foreach($toProgress as $p)
$this->replaceNodeWithString($p['item'],$p['data']);
}
//echo "znalazł imprtow:".$i;
}
private function applyValues()
{
$elements = $this->DOMDoc->getElementsByTagNameNS($this->NS, "value");
$i=0;
foreach($elements as $item)
{
if(substr($item->getAttribute("select"),0,1)!="@") {
$value = $this->select($item->getAttribute("select"),$item->getAttribute("default"));
$toProgress[$i]['item']=$item;
$toProgress[$i]['data']=$value;
}
$i++;
}
foreach($toProgress as $p)
$this->replaceNodeWithString($p['item'],$p['data']);
}
private function applyLoops()
{
$elements = $this->DOMDoc->getElementsByTagNameNS($this->NS, "loop");
foreach($elements as $item)
{
if($name = $item->getAttribute("name"))
{
if($item->getAttribute("select") or $item->getAttribute("default"))
{
$value = $this->select($item->getAttribute("select"),$item->getAttribute("default"));
$uStart = (int)$item->getAttribute("start");
$uStart?$start=$uStart:$start=0;
$uMax = (int) $item->getAttribute("max");
$uMax?
$max=$uMax:$max=count($value); $uStep = (int) $item->getAttribute("step");
$uStep?$step=$uStep:$step=1;
//ponizej utknalem i nie wiem jak to pociagnac
for($i=$start;$i<=$max;$i+=$step)
{
}
}
else
throw new Exception();
}
else
throw new Exception();
}
}
private function removeNS($tpl)
{
}
private function applyTests()
{
$elements = $this->DOMDoc->getElementsByTagNameNS($this->NS, "test");
}
public function applyTemplates()
{
$this->applyImports();
$this->applyValues();
$this->applyLoops();
echo $this->removeNS($this->DOMDoc->saveXML()); }
}
class obj
{
public $my = "moja wartosc obiektu";
}
$tpl = new Template("tpl.tpl","1.0","utf-8");
$tpl->vars = array("new"=>"Asasasasasaa new","ar"=>$ar); $tpl->applyTemplates();
?>
szablon
<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:tpl="http://someexaple.com" > <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<tpl:value select="//new" default="#cos"/>
<div id="cos">as dasd asds sd
</div> <tpl:loop name="my" select=".a" default="//ar" start="2">
<tpl:value select="//new" default="#cos"/>
<div style="color:#FF0000">data:
<tpl:value select="@my/index"/></div> <tpl:value select="@my/ar"/>
</tpl:loop>
W pliku php zaznaczylem w ktorym miejscu utknalem, chcialem zrobic petle w taki sposob zeby w srodku niej mozna sie bylo odwolac do nadanej wczesniej tablicy "ar" poprzez <tpl:value select="@my/ar"/> i zeby reszta dokumentu ktory jest we wnatrz nie znikala. Ma ktos jakis pomysl, juz mecze to chyba z tydzien i nic

klasa oczywiscie jesze nie jest skonczona wiec zmienne nadaje sie narazie tak jak sie nadaje pozniej to zrobie na przeladowaniach
Ten post edytował deirathe 26.01.2008, 15:38:53