消息“未找到错误”的未捕获异常“ DOMException” [英] Uncaught exception 'DOMException' with message 'Not Found Error'
问题描述
基本上,我正在为CMS写一个模板系统,我希望有一个模块化的结构,其中涉及到人们在其中放置标签的情况,例如:
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:
<模块名称= news />
或< include name = anotherTemplateFile />
在我的php中,并替换为动态html。
<module name="news" />
or <include name="anotherTemplateFile" />
which I then want to find in my php and replace with dynamic html.
此处有人将我指向DOMDocument,但我已经遇到了问题。
Someone on here pointed me towards DOMDocument, but I've already come across a problem.
我试图在模板中找到所有< include />
标记,并用一些简单的html替换它们。这是我的模板代码:
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>
这是我的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();
现在,我收到致命错误未捕获的异常'DOMException',消息为'找不到错误'
。
Now, I get the fatal error Uncaught exception 'DOMException' with message 'Not Found Error'
.
我已经查过了,这似乎是因为我的< include />
标记不一定是 $ template
的直接子代,而不替换它们。
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?
谢谢
汤姆
编辑
基本上我有各种各样的想法。如果我为PHP做类似的事情,我会看到它正在尝试做我想做的事情:
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();
不过,它似乎只改变了< body>