]> git.sesse.net Git - xml-template/commitdiff
Add include support to the PHP version.
authorsgunderson@bigfoot.com <>
Sat, 12 Aug 2006 00:00:35 +0000 (02:00 +0200)
committersgunderson@bigfoot.com <>
Sat, 12 Aug 2006 00:00:35 +0000 (02:00 +0200)
include.php [new file with mode: 0644]
xml-template.php

diff --git a/include.php b/include.php
new file mode 100644 (file)
index 0000000..a4b0ab3
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+require('xml-template.php');
+
+$doc = XML_Template_process_file('included.xml', array(
+       'color' => 'red'
+));
+$master = XML_Template_process_file('master.xml', array(
+       'title' => 'Main HTML title',
+       'h1' => 'Nice heading here',
+       'contents' => $doc
+));
+print $master->dump_mem();
+?>
index 5c3d2b492e85f045d0e4eab6125c3895104515db..48837c7264eb01ace3c27dbfea17fe5cce46de21 100644 (file)
@@ -9,13 +9,23 @@ function XML_Template_process_file($filename, $obj, $clean = 1)
 
 function XML_Template_process($node, $obj, $clean = 1)
 {
-       if (!is_array($obj)) {                                # overwrite
+       if (is_a($obj, 'domnode')) {                          # overwrite
+               foreach ($node->child_nodes() as $child) {
+                       $node->remove_child($child);
+               }
+
+               if (is_a($obj, 'domdocument')) {
+                       $obj = $obj->document_element();
+               }
+
+               $newobj = $obj->clone_node(true);
+               $node->append_child($newobj);
+       } else if (!is_array($obj)) {                         # overwrite
                foreach ($node->child_nodes() as $child) {
                        $node->remove_child($child);
                }
                $doc = $node->owner_document();
                $node->add_child($doc->create_text_node($obj));
-       # handle overwrite with a DOM object here
        } else if (is_associative_array($obj)) {              # substitute
                foreach ($node->child_nodes() as $child) {
                        $processed = false;