将 PHP 和 HTML 代码加载到中回显的 PHP 变量中。
这是我的案例。我有以下脚本:
<?php
// ... other code ...
switch($_GET['request']) {
case "firstPage":
$file = APP_ROOT. "pages/firstPage.php";
$hndle = fopen($file,"r");
$right_column = fread($hndle, filesize($file));
fclose($hndle);
break;
case "secondPage":
$file = APP_ROOT. "pages/secondPage.php";
$hndle = fopen($file,"r");
$right_column = fread($hndle, filesize($file));
fclose($hndle);
break;
}
}
?>
<div id="right_column">
<?php echo $right_column;?>
</div>
// ... other code ...
根据 $_GET['request'] 的值,我将 php 文件的内容分配给变量 $right_column 。然后,我在最后一个 div 中回显该变量。 firstPage.php 和 secondaryPage.php 文件包含混合的 html 和 php 代码。我寻找像 Zend 中的“部分”这样的解决方案。谢谢
Here is my case. I have the following script:
<?php
// ... other code ...
switch($_GET['request']) {
case "firstPage":
$file = APP_ROOT. "pages/firstPage.php";
$hndle = fopen($file,"r");
$right_column = fread($hndle, filesize($file));
fclose($hndle);
break;
case "secondPage":
$file = APP_ROOT. "pages/secondPage.php";
$hndle = fopen($file,"r");
$right_column = fread($hndle, filesize($file));
fclose($hndle);
break;
}
}
?>
<div id="right_column">
<?php echo $right_column;?>
</div>
// ... other code ...
Depending on the value of the $_GET['request'] I am assigning to the variable $right_column the content of a php file. Then, I echo that variable in the last div. The firstPage.php and secondPage.php files contain mixed html and php code. I look for a solution like the 'partial' in Zend. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,你的代码很糟糕,它应该是这样的:
请阅读 http://codeangel.org/articles/simple-php-template- engine.html ,您可能会发现这很有趣。它应该向您解释如何轻松利用 php 的本机模板功能。
First of all , your code is horrible, it should be something like :
An please read http://codeangel.org/articles/simple-php-template-engine.html , you might find this interesting. It should explain to you , how you can easily take advantage of php's native templating capabilities.
实际上,读取并转储文件并不是解决方案。您只是向最终用户显示代码。
您需要的是解析 PHP 文件并显示结果。您可以通过两种方式做到这一点:
首先,
您可以这样做:
因此,您的firstPage.php 必须返回代码。像这样的东西:
但您也可以像这样包含它:
或者,您可以使用ob_get_contents。 PHP 有一个关于如何使用它的很好的文档。
我会用 include 来处理你的情况。
Actually, reading and dumping your file is not a solution. You are just displaying code to your end-user.
What you need is parse the PHP file and display the result. You can do it in 2 ways:
First, intead of
You can do:
So, your firstPage.php must RETURN the code. Something like this:
But you also can include it like this:
Or, you can use the ob_get_contents. PHP has a nice documentation about how to use it.
I'd use include for your case.
如果您设置一个变量来指示要包含哪个 PHP 文件,然后不读取该文件,只需包含它,这可能会更简单。例如:
如果您的firstPage.php 和secondPage.php 文件只有HTML,那么您所做的事情就可以工作。如果它有 PHP,那么您需要
include()
它;或者你可以eval()
它,但这又做了比你需要的更多的工作。It would probably be simpler if you set a variable indicating which PHP file to include, and then rather than reading the file, just include it. For example:
If your firstPage.php and secondPage.php files only had HTML, what you're doing would work. If it has PHP, then you'll need to
include()
it; or you couldeval()
it but that is again doing more work than you need.在我看来,您可以按照约定进行编程,而不是不断增加 switch 语句:
It seems to me that you could program by convention rather than have an ever increasing switch statement: