簡體   English   中英

未捕獲的異常'DOMException',消息'Not Found Error'

[英]Uncaught exception 'DOMException' with message 'Not Found Error'

基本上我正在為我的CMS編寫一個模板系統,我想要一個模塊化的結構,讓人們輸入標簽,如:

<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();

然而它似乎只改變我的HTML的<body>的1個出現...當我有3.:/

這是DOMDocument-> load的問題,請嘗試

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

但是你可能需要給它一個.html擴展名。

這是在尋找一個html或xml文件。 另外,每當你使用帶有html的DOMDocument時,最好使用libxml_use_internal_errors(true); 在加載調用之前。

好的,這個工作:

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();

然而它似乎只改變了我的HTML中的1個出現...當我有3.:/

DOM NodeLists是“實時”:當您從文檔中刪除<include>元素(通過替換它)時,它將從列表中消失。 相反,如果您在文檔中添加新的<include> ,它將顯示在您的列表中。

對於來自元素的childNodes的NodeList,您可能會期望這樣,但返回getElementsByTagName的NodeLists也是如此。 它是W3C DOM標准的一部分,發生在Web瀏覽器的DOM以及PHP的DOMDocument中。

所以你在這里有一個破壞性的迭代。 刪除第一個<include> (列表中的項目0),第二個<include> ,先前的項目1,成為新項目0.現在當您轉到列表中的下一個項目時,項目1是以前的項目第2項,導致你只看一半的項目。

PHP的foreach循環看起來像它可以保護您從,但實際上在幕后它做完全一樣的傳統索引for循環。

我會盡量避免為PHP創建一個新的模板語言; 已經有這么多了,更不用說PHP本身了。 用DOMDocument創建一個也會特別慢。

eta:一般來說,正則表達式替換會更快,假設一個簡單的匹配模式不會引入回溯負載。 但是,如果您堅持使用XML語法,那么正則表達式在解析它時並不是很好。 但是你想做什么,用PHP已經不能做了?

<?php function write_header() { ?>
    <p>This is the header bit!</p>
<? } ?>

<body>
    ...
    <?php write_header(); ?>
    ...
</body>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM