如何正确使用PHP模板

发布于 2025-02-08 12:51:33 字数 1000 浏览 1 评论 0原文

我是PHP的新手,目前正在浏览“绝对初学者的PHP”书。在这本书中,它目前正在教授有关模板和使用stdclass()对象以避免命名冲突的信息。

我有一个用于模板的文件,称为page.php,我的主页称为index.php。

我的page.php代码

<?php
return "<!DOCTYPE html>
<html>
<head>  
<title>$pageData->title</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
$pageData->$content
</body>
</html>";

我的index.php

<?php
//this correctly outputs any errors 
error_reporting( E_ALL );
ini_set("display_errors", 1);

$pageData = new stdClass();

$pageData->title = "Test title";
$pageData->content = "<h1>Hello World</h1>";
$page = include_once "templates/page.php";
echo $page;

我要收到的错误是

警告:c:\ xampp \ htdocs \ htdocs \ ch2 \ templates \ page.php in 5 line 5

警告:c:\ xampp中未定义的变量$内容\ htdocs \ ch2 \ templates \ pag.php在第9行上

我不明白这一点,因为这正是这本书正在教授任何帮助,如果有任何更好的方法使用模板,请记住我是初学者,请记住因此,请保持简单!

I am new to PHP and I'm currently working through 'PHP for absolute beginners' book. In the book it is currently teaching about templating and using the StdClass() Object to avoid naming conflicts.

I have a file for templating called page.php and a file for my homepage called index.php.

My page.php code

<?php
return "<!DOCTYPE html>
<html>
<head>  
<title>$pageData->title</title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
$pageData->$content
</body>
</html>";

My index.php

<?php
//this correctly outputs any errors 
error_reporting( E_ALL );
ini_set("display_errors", 1);

$pageData = new stdClass();

$pageData->title = "Test title";
$pageData->content = "<h1>Hello World</h1>";
$page = include_once "templates/page.php";
echo $page;

The errors i am receiving are

Warning: Undefined variable $title in C:\xampp\htdocs\ch2\templates\page.php on line 5

Warning: Undefined variable $content in C:\xampp\htdocs\ch2\templates\page.php on line 9

I don't understand this as it is exactly what the book is teaching any help would be appreciated and please if there are any better ways to use templating please remember I am a beginner so keep it simple!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日 2025-02-15 12:51:33

您的page.php看起来有些奇怪。 返回在打印HTML的上下文中不是我会使用的。另外,您正在使用PHP标签中的PHP和HTML,无法正常工作。尝试以下操作:

<!DOCTYPE html>
<html>
<head>  
<title><?php echo $pageData->title; ?></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
<?php echo $pageData->content; ?>
</body>
</html>

您不需要echo $ page在您的index.php中,让page.php执行此操作。

/edit:第9行中还有一个错字:$ pagdata-&gt; $ content;我对此进行了更正。

Your page.php looks a little odd. return is not something I would use in context of printing html. Also, you are using php and html within the php tag which can't work. Try this:

<!DOCTYPE html>
<html>
<head>  
<title><?php echo $pageData->title; ?></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'/>
</head>
<body>
<?php echo $pageData->content; ?>
</body>
</html>

You don't need echo $page in your index.php, let the page.php do that.

/Edit: There is also a typo in line 9: $pageData->$content; I corrected that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文