Jezeli macie ochote to przetestujcie ten system i napiszcie co o tym sadzicie. Ewentualne pytania dot dzialania i dokladnego uzycia klasy na PM
plik klasy:
<?php require_once("Template/Exception.class.php"); class Template{ public $xml; public $xml_version; public $xml_encoding; public $xsl; public $xsl_path; public $cacheDir = "cache/"; public $xslContents; protected $xslSum; protected $xmlSum; public $cache=false;//ze wzgledu na xsl:imports =/ trzeba pomyslec jak wypelnic wszystkie zmienne w tpl jakimis defaultami private $xslSheet; private $xslProcessor; public $allowPHP=false; public $dictionaryDir="./"; private $dictionaryFilePath; private function is_array_asc($array) { { return true; } return false; } public function setLanguage($lang=null) { if($this->dictionaryFilePath) throw new Template_Exception("Language file already defined!"); $this->dictionaryFilePath=$lang.".xml"; $this->dictionaryFilePath=$this->dictionaryDir.$lang.".xml"; else throw new Template_Exception("Language file doesn't exists"); $documentfragment = $this->xml->createDocumentFragment(); $this->xml->documentElement->appendChild($documentfragment); } public function __construct($xsl=null,$allowPHP=null,$xml_version=null,$xml_encoding=null) { $allowPHP?$this->allowPHP=true:$this->allowPHP=false; $xml_version?$this->xml_version=$xml_version:$this->xml_version="1.0"; $xml_encoding?$this->xml_encoding=$xml_encoding:$this->xml_encoding="UTF-8"; //create DOMDocument $this->xml = new DOMDocument($this->xml_version,$this->xml_encoding); $this->xml->appendChild($this->xml->createElement('root')); if($xsl) $this->setTemplate($xsl); } public function setTemplate($xsl) { { $this->xsl_path=$xsl; } { $this->xsl_path=$xsl; } else { throw new Template_Exception("File:".$xsl." doesn't exists"); } $this->xslSheet = new DomDocument; $this->xslSheet->loadXML($this->xslContents); $this->xslSheet->documentURI = $this->xsl_path; $this->xslProcessor = new XSLTprocessor(); $this->xslProcessor->setParameter('',"xmlns",'"http://www.w3.org/1999/xhtml'); //jezeli zezwolimy templakowi na uzywanie funkcji php to mozemy je w templacie //wykonac nastepujaco : <xsl:value-of select="php:function('nl2br',string(MessageContent/Message))" disable-output-escaping="yes"/> //lub tez <xsl:value-of select="php:function('dateLang')" /> /* <?php function getNodeSet() { $xml = "<test>" . "<a-node>This is a node</a-node>" . "<a-node>This is another node</a-node>" . "</test>"; $doc = new DOMDocument; $doc->loadXml($xml); return $doc; } ?> <xsl:template match="/"> <xsl:apply-templates select="php:function('getNodeSet')" /> </xsl:template> <xsl:apply-templates select="php:function('getNodeSet')" mode="discardRoot" /> */ if($this->allowPHP) { //xmlns:php="http://php.net/xsl" $this->xslProcessor->setParameter('', 'xmlns', 'asdasd' ); $this->xslProcessor->registerPHPFunctions(); } $this->xslProcessor->importStyleSheet($this->xslSheet); //print_r($this->xslProcessor->transformToDoc($this->xml)->saveXML()); //to moze juz posluzyc jako cache ale brakuje mi zmiennych :( //niestety tymczasowo cache trzeba usunąć i pomyśleć nad tym, zostawiam jako opcje ale narazie lepiej nie uzywac //cache ponizej if($this->cache) } private function array_to_xml($key,$value) { if($this->is_array_asc($value)) { $data .= "<".$key.">"; foreach($value as $k => $v) { $data.=$this->array_to_xml($k,$v); else $data.="<$k>".$v."</$k>"; } $data .= "</".$key.">"; } else { foreach($value as $v) { $data .= "<".$key.">"; $data.=$this->array_to_xml($k,$v); else $data.="<value>".$v."</value>"; $data .= "</".$key.">"; } } return $data; } /* settig values */ public function set($key,$value) { { $documentfragment = $this->xml->createDocumentFragment(); $documentfragment->appendXML($this->array_to_xml($key,$value)); $this->xml->documentElement->appendChild($documentfragment); } else $this->xml->documentElement->appendChild($this->xml->createElement($key,$value)); } public function __set($key,$value) { $this->set($key,$value); } public function setDictionaryDir($dir) { $this->dictionaryDir = $dir; } private function touchCache($filePath) { } public function checkCache() { return true; else { $this->touchCache($this->cacheDir.$this->xmlSum); return false; } else { $this->touchCache($this->cacheDir.$this->xslSum); return false; } } private function saveXhtml($content) { } public function applyTemplate() { if(!$this->xsl_path) throw new Template_Exception("Template file not set"); if($this->cache) { if($this->checkCache()) else { $xhtml = $this->xslProcessor->transformToXML($this->xml); $this->saveXhtml($xhtml); } } else { $xhtml = $this->xslProcessor->transformToXML($this->xml); } //echo $this->xml->saveXML(); } } ?>
plik: Template/Exception.class.php
<?php class Template_Exception extends Exception { public function __construct($message=null, $code=0) { } } ?>
plik index.php
<?php include "Template.class.php"; for($i=0;$i<10;$i++){ } $tpl = new Template("tpls/tpl.xsl",true); $tpl->setDictionaryDir("Dictionary/"); $tpl->setLanguage("pl"); $tpl->loop=$data;//wyslanie do templaka zmiennej loop o wartosci data $tpl->title="asa";//wylanie do templaka zmiennej asa $tpl->applyTemplate(); ?>
plik szablonu tpl.xsl:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:php="http://php.net/xsl"> <xsl:import href="plugin.xsl"/> <xsl:output method="xml" version="1.0" media-type="text/html" doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" encoding="utf-8"/> <xsl:template match="/"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> <xsl:value-of select="//title" /> </title> </head> <body> <p> <xsl:apply-imports /> <xsl:for-each select="//loop"> <xsl:value-of select="value" />data<br /> </xsl:for-each> </p> </body> </html> </xsl:template> </xsl:stylesheet>
plik plugin.xsl
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl"> <xsl:output method="html"/> <xsl:template match="/"> <b><xsl:value-of select="php:function('strstr',string(//title),'t')" /></b> <span><xsl:value-of select="//dictionary/b2" /></span> </xsl:template> </xsl:stylesheet>
plik: Dictionary/pl.xml
<dictionary> <b2> bądź dwa </b2> </dictionary>
----------------------
nadal pracuje nad klasa, wymaga stworzenia cache, jezeli ktos bedzie mial pomysl to prosilbym o kontakt deirathe@gmail.com lub PM