UltraMega Blog
21Jul/095

Modifying Templates Using DOMDocument In PHP

In the previous post, Generating HTML5 Documents Using DOMDocument In PHP, we explored the PHP DOMDocument class by generating an HTML5 page completely within PHP. Now, we'll look at a more practical application that involves modifying an existing template. The template gives us a good starting point so we can focus on generating only the dynamic parts of the page.

So here is the template we will be modifying. It is identical to the page we generated, except that the title and content are removed. We will insert a title and content with PHP.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html lang="en">
<head>
<meta content="charset=utf-8" />
<title></title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div id="wrapper">
  <div id="header"><img src="header.gif" alt="Header" width="400" height="100" /></div>
  <div id="nav">
    <ul>
      <li><a href="index.php">Home</a></li>
      <li><a href="download.php">Download</a></li>
      <li><a href="features.php">Features</a></li>
      <li><a href="about.php">About</a></li>
    </ul>
  </div>
  <div id="content"></div>
</div>
</body>
</html>

Now for the code. First, we need to load the template document, called template.htm, with the DOMDocument::loadHTMLFile method.

1
2
3
4
<?php
// Load the template file into a new DOMDocument
$document = new DOMDocument();
$document->loadHTMLFile('template.htm');

Here we create the title text and insert it into the title tag. We grab the first "title" element and append the text node.

6
7
8
// Set the page title
$title = $document->createTextNode('Page Title');
$document->getElementsByTagName('title')->item(0)->appendChild($title);

Find the "content" div so we can add our content:

10
11
// Access the content div
$content = $document->getElementById('content');

Here is where we generate the content:

13
14
15
16
17
18
19
20
21
22
23
24
// Insert actual page content into content div
$h1 = $document->createElement('h1', 'DOMDocument Template');
$content->appendChild($h1);
 
$p = $document->createElement('p');
$text = $document->createTextNode('This template was modified using PHP\'s ');
$p->appendChild($text);
$text = $document->createElement('strong', 'DOMDocument');
$p->appendChild($text);
$text = $document->createTextNode(' class!');
$p->appendChild($text);
$content->appendChild($p);

Now we output the modified document:

26
27
28
// Output document
$document->formatOutput = true;
echo $document->saveHTML();

Check out the demo page...

Posted by Steve

Tagged as: , , Leave a comment
Comments (5) Trackbacks (2)
  1. didn’t work at all man…. or maybe my browser doesn’t support that version of the xml document.

    • If you are referring to the demo, you should just see the markup as text. That’s intentional so you see the actual code generated.

  2. Man !! you did it very nice !!

    Thank you very mucho !!

  3. Hi ,

    Its worked fine in html pages but not in PHP.

    I have tried to append some content with PHP file . its not worked.

    Please help me for appending php file content to another php file .

  4. Hi, I would like to ask you if using this technique to use as the way to modify html templates in a site would be very slow? I am thinking of three possibilities: loading html with file_get_contents and modifying the templates with str_replace, or using only PHP to generate html (I know this is against of having php code and html separate), or using phpQuery to modify the html templates. I am tempted to use the last approach, but I am afraid that this would cause my sites to load slowly. I think phpQuery uses DOM, so I think it has more overhead. I am concerned about memory usage too. I know that is the never ending dilemma of using APIs and frameworks. If this is not going to be very slow, I would like to implement this to enjoy the power it would give, to implement a whole plugin system, etc.
    Thanks.


Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.