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