Bascially我正在写一个模板系统为我的CMS和我想有一个模块化结构,涉及人的标签把这样的:未捕获的异常“抛出:DOMException”有消息“未找到错误”
<module name="news" />
或<include name="anotherTemplateFile" />
这然后我想在我的php中找到并用动态html替换。
有人在这里指出我对DOMDocument,但我已经遇到了一个问题。
我正试图在我的模板中找到所有<include />
标签,并用一些简单的html替换它们。这里是我的模板代码:
<!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();
现在,我得到的致命错误Uncaught exception 'DOMException' with message 'Not Found Error'
。
我看这件事,似乎是因为我<include />
标签不一定是$template
DIRECT儿童它不是取代他们。
如何独立于血统来替换它们?
谢谢
汤姆
编辑
基本上我有各种各样的脑电波。如果我做这样的事情对我的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);
// this line is different:
$include->parentNode->replaceChild($output, $include);
}
echo $template->saveHTML();
但是似乎只改变1个occurence在我的HTML的<body>
......当我有3个。:/
尝试http://simplehtmldom.sourceforge.net/而不是DOM文档 – yoda 2011-03-12 17:26:44
@yoda你告诉他使用一个沉闷的黄油刀,而不是scalpell – Gordon 2011-03-12 17:32:34
@Gordon的:它的工作原理呢。 – yoda 2011-03-12 17:33:26