繁体   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