index.php
<?php
class Template
{
private $DOMDoc;
public $vars;
public $templateDir;
public $NS="http://someexaple.com";
public function __construct($templateFile=null,$version=null,$encoding=null)
{
$this->DOMDoc = new DOMDocument($version,$encoding);
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)
{
if(!$path)
throw new Exception();
switch($type)
{
case "@":
//petle
break;
case "#":
//dokument->xpath
break;
case "/":
$input = $this->vars;
foreach($path as $route)
$input = $input[$route];
break;
default:
throw new Exception("nie prawdilowae odniesienie do zmiennej:".$path." o typie:'".$type."'");
break;
}
return $input;
}
private function replaceNodeWithString($oldNode,$xmlString)
{
$f = $this->DOMDoc->createDocumentFragment();
if($f->appendXML($xmlString))
$oldNode->parentNode->replaceChild($f,$oldNode);
}
/*w przyszlosci doda podmienianie wsztsrkich tagow odnoszacych sie do tych samych
plikow*/
private function applyImports()
{
$elements = $this->DOMDoc->getElementsByTagNameNS($this->NS, "import");
foreach($elements 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)
{
{
$this->replaceNodeWithString($item,$text);
}
else
throw new Exception();
}
else
throw new Exception();
}
}
private function applyValues()
{
$elements = $this->DOMDoc->getElementsByTagNameNS($this->NS, "value");
foreach($elements as $item)
{
if(substr($item->getAttribute("select"),0,1)=="/") {
$value = $this->select($item->getAttribute("select"));
$this->replaceNodeWithString($item,$value);
}
}
}
public function applyTemplates()
{
$this->applyImports();
$this->applyValues();
echo $this->DOMDoc->saveXML(); }
}
$tpl = new Template("tpl.tpl","1.0","utf-8");
$tpl->vars = array("asd"=>"wartos dla zmiennej asd","as4"=>array
("as2"=>"wartosc dla as4/as2"),"new"=>"plug.tpl"); $tpl->applyTemplates();
?>
tpl.tpl
<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:tpl="http://someexaple.com" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><tpl:import select="//new" /></title>
</head>
<body>
<tpl:import select="//new" />
<tpl:import select="//new" />
<tpl:import select="//asd" />
<tpl:import select="//new" />
<tpl:value select="//asd" />
<tpl:foreach select="//asd2"> sdfds
<tpl:foreach id="my" select="/aaa1" start="/zmienna" stop="10">
<tpl:lang href="</span>">
<tpl:value select="@my/keys/1"/>
</tpl:lang>
</tpl:foreach>
<tpl:import select="//new" />
</tpl:foreach>
<tpl:il8n select="//dict/asa"/>
<tpl:value select="//as4/as2" /> <tpl:value select="/as4/as2" />
<tpl:test not="value" equal="value,value" isset="value">
<tpl:else not="" equal="" isset="</span>"></tpl:else>
<tpl:else not="" equal="" isset="</span>"></tpl:else>
</tpl:test>
<tpl:switch select="</span>">
<tpl:case value="">
<tpl:import select="<<span style='color:blue'>span style='color:orange'>//new" />
</tpl:case>
</tpl:switch>
<tpl:import href="plug.tpl" />
</body>
</html>
plug.tpl
<b>plugin lub cos</b>
Problem polega na tym że klasa mi nie chce podmieniać wszystkich includow z templaka, podmienia tylko niektore na co wskazuje literka a ktora powtarza sie tyle razy ile jest includow w pliku tpl.tpl,
natomiast gdy zrobie to tak:
<?php
$tpl = new DOMDocument("1.0");
$tpl->load("tpl.tpl");
$e = $tpl->getElementsByTagNameNS("http://someexaple.com","import");
foreach($e as $i)
?>
to wykrywa wszystkie includy. Co moze byc zgrane?
tutaj mozna znalezc wlaczona klaseDzieki z gory za pomoc
Ten post edytował deirathe 22.01.2008, 14:49:53