gpt4 book ai didi

domdocument - 未捕获的异常 'DOMException',消息为 'Not Found Error'

转载 作者:可可西里 更新时间:2023-11-01 01:09:02 28 4
gpt4 key购买 nike

基本上我正在为我的 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();

现在,我得到了 fatal error Uncaught exception 'DOMException' with message 'Not Found Error' .

我查了一下,似乎是因为我的 <include />标签不一定是 $template 的直接子代它不会取代它们。

我怎样才能独立于血统替换它们?

谢谢

汤姆

编辑

基本上我有各种各样的灵感。如果我为我的 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();

然而它似乎只改变了 <body> 中的 1 次出现我的 HTML...当我有 3 个时。:/

最佳答案

这是你DOMDocument的问题->加载,试试

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

但是您可能需要给它一个 .html 扩展名。

这是在寻找 html 或 xml 文件。此外,无论何时将 DOMDocument 与 html 一起使用,最好在加载调用之前使用 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();

关于domdocument - 未捕获的异常 'DOMException',消息为 'Not Found Error',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5284131/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com