Wie versprochen kommen nun noch einige Details zum „Grabben“ des Themes.
<?php class GrabIt_Xml_Helper { protected $prePlaceHolders = array( 'contentBlock-1' => '##CONTENT-1###', 'contentBlock-2' => '##CONTENT-2###', 'contentBlock-3' => '##CONTENT-3###', 'feature' => '##FEATURE###', ); protected $postPlaceHolders = array( '##CONTENT-1###' => 'content-1', '##CONTENT-2###' => 'content-2', '##CONTENT-3###' => 'content-3', '##FEATURE###' => 'feature', '##FOOTER###' => 'footer', '##MENU###' => 'menu', ); protected $debug = false; function __construct($url) { $this->setUrl($url); } function setUrl($url) { $this->url = $url; } function setPrePlaceHolders($array) { $this->prePlaceHolders = $array; } function setPrePlaceHolder($name, $value) { $this->prePlaceHolders[$name] = $value; } function setPostPlaceHolder($name, $value) { $this->postPlaceHolders[$name] = $value; } function parseDocument() { if(!$this->debug === true) { $display_errors = ini_get('display_errors'); ini_set('display_errors', 0); } $this->dom = new DOMDocument(); $this->dom->loadHTMLFile($this->url); foreach($this->prePlaceHolders as $newNodeId => $placeHolder) { $this->replaceNode($newNodeId, $placeHolder); } if(!$this->debug === true) { ini_set('display_errors', $display_errors); } return $this->getReadyDocumentAsString(); } function replaceNode($newNodeId, $placeHolder) { $nodeToReplace = $this->dom->getElementById($newNodeId); if($nodeToReplace) { $nodeToReplaceParent = $nodeToReplace->parentNode; $newNode = new DOMElement('div', $placeHolder); $nodeToReplaceParent->replaceChild($newNode, $nodeToReplace); $newNode->setAttribute('id', $newNodeId); } } function getReadyDocumentAsString() { $buffer = $this->dom->saveHTML(); foreach($this->postPlaceHolders as $newNodeId => $placeHolder) { $buffer = str_replace($newNodeId, $placeHolder, $buffer); } return $buffer; } }
Dank dieser Klasse können einzelne XML Nodes ersetzt werden. Dabei gehe ich zwei Schrittig vor.
- Werden die Nodes mit Platzhaltern gefüllt, z.B. ###CONTENT-1###
- Im zweiten Schritt kann an die Stelle der Platzhalter normaler HTML Code gesetzt werden
Die zwei Schritte sind nötig, da ansonsten der einzufügende HTML Code valides XML sein muss, da XMLDOM sonst Exceptions wirft.
Um die Klasse zu Verwenden ist dann noch folgender Code nötig:
define('WP_DEBUG', true); include_once('Classes/Util/Functions.php'); $options = GrabIt_Backend_Setup::getOptions(); $helper = new GrabIt_Xml_Helper($options['layoutUri']); $helper->setPostPlaceHolder('##CONTENT-1###', UtilFunctions::renderSiteContent('page')); $helper->setPostPlaceHolder('##CONTENT-2###', UtilFunctions::renderSiteContent('sidebar1')); $helper->setPostPlaceHolder('##CONTENT-3###', UtilFunctions::renderSiteContent('sidebar2')); $helper->setPostPlaceHolder('>/head<' , UtilFunctions::renderSiteContent('head')); echo $helper->parseDocument();
In den nächsten Tagen werde ich noch folgendes einbauen:
- Umwandlung relativer Links in absolute Links 😉
- Erlauben alternativer Settings zu TV Framework und den darauf basierenden Themes
Cheers