Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4056110
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:47:08+00:00 2026-05-20T14:47:08+00:00

Bascially I’m writing a templating system for my CMS and I want to have

  • 0

Bascially I’m writing a templating system for my CMS and I want to have a modular structure which involves people putting in tags like:

<module name="news" /> or <include name="anotherTemplateFile" /> which I then want to find in my php and replace with dynamic html.

Someone on here pointed me towards DOMDocument, but I’ve already come across a problem.

I’m trying to find all <include /> tags in my template and replace them with some simple html. Here is my template code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>CMS</title>
<include name="head" />
</head>

<body>

<include name="header" />

<include name="content" />

<include name="footer" />

</body>
</html>

And here is my PHP:

$template = new DOMDocument();
$template->load("template/template.tpl");

foreach( $template->getElementsByTagName("include") as $include ) {
    $element = '<input type="text" value="'.print_r($include, true).'" />';
    $output = $template->createTextNode($element);
    $template->replaceChild($output, $include);
}

echo $template->saveHTML();

Now, I get the fatal error Uncaught exception 'DOMException' with message 'Not Found Error'.

I’ve looked this up and it seems to be that because my <include /> tags aren’t necessarily DIRECT children of $template its not replacing them.

How can I replace them independently of descent?

Thank you

Tom

EDIT

Basically I had a brainwave of sorts. If I do something like this for my PHP I see its trying to do what I want it to do:

$template = new DOMDocument();
    $template->load("template/template.tpl");

    foreach( $template->getElementsByTagName("include") as $include ) {
        $element = '<input type="text" value="'.print_r($include, true).'" />';
        $output = $template->createTextNode($element);
        // this line is different:
        $include->parentNode->replaceChild($output, $include);
    }

    echo $template->saveHTML();

However it only seems to change 1 occurence in the <body> of my HTML… when I have 3. :/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:47:08+00:00Added an answer on May 20, 2026 at 2:47 pm

    This is a problem with your DOMDocument->load, try

    $template->loadHTMLFile("template/template.tpl"); 
    

    But you may need to give it a .html extension.

    this is looking for a html or an xml file. also, whenever you are using DOMDocument with html it is a good idea to use libxml_use_internal_errors(true); before the load call.

    OKAY THIS WORKS:

    foreach( $template->getElementsByTagName("include") as $include ) {
         if ($include->hasAttributes()) {
         $includes[] = $include;
         }
         //var_dump($includes);
     }
     foreach ($includes as $include) {
                $include_name = $include->getAttribute("name");
            $input = $template->createElement('input');
    $type = $template->createAttribute('type');
    $typeval = $template->createTextNode('text');
    $type->appendChild($typeval);
    $input->appendChild($type);
    $name = $template->createAttribute('name');
    $nameval = $template->createTextNode('the_name');
    $name->appendChild($nameval);
    $input->appendChild($name);
    $value = $template->createAttribute('value');
    $valueval = $template->createTextNode($include_name);
    $value->appendChild($valueval);
    $input->appendChild($value);
    if ($include->getAttribute("name") == "head") {
           $template->getElementsByTagName('head')->item(0)->replaceChild($input,$include);
    }
    else {
            $template->getElementsByTagName("body")->item(0)->replaceChild($input,$include);
    }
            }
            //$template->load($nht);
            echo $template->saveHTML();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which is bascially a TabBar where the tabs are UINavigationControllers
Bascially you have first to do a: SurfFeatureDetector surf(400); surf.detect(image1, keypoints1); And then a:
i bascially want to get the value of a variable from the codebehind to
Being fairly new to C++ I have a question bascially concerning the g++ compiler
Say if I have the code below, it's bascially determining some condition is matched
I'm quite new to Node.js, so please bear with me... Bascially, I have a
I have a model which is referenced to a table registries extending Zend_Db and
What i bascially want is to extend the Numeric class so that it has
I have made a simple timer app which update a textview every second. I
I have a class which looks like as follows: public class MyConstants { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.