>我在HTML代码中运行php代码,但似乎php代码加载然后是HTML。
我的网页:
<div id ="main-Body">
<?php $this->load($content);?> //$content: link file
</div>
函数负载:
public function load($view,$data =[])
{
extract($data);
ob_start();
require_once PATH_APPLICATION . '/views/' . $view . '.php';
$content = ob_get_contents();
ob_end_clean();
$this->loadedContent[] = $content;
}
public function Show()
{
foreach($this->loadedContent as $html)
{
echo $html;
}
}
浏览器上的结果:
<!--It shows $this->load($content) here-->
<div id ="main-Body">
<!--nothing here-->
</div>
编辑:如果我将以下代码放入$content文件
<div id ="main-Body">
</div>
然后显示:
<?php $this->load($content);?> //$content: link file
这没有错。
从函数返回 load内容:
public function load($view,$data =[])
{
extract($data);
ob_start();
require_once PATH_APPLICATION . '/views/' . $view . '.php';
$content = ob_get_contents();
ob_end_clean();
$this->loadedContent[] = $content;
return $this->loadedContent;
}
我现在刚刚看到了新更新的问题....除非您使用的是隐式调用 Show 方法的框架,否则我会说您不会在代码中的任何位置调用 Show 方法......从您的代码中,Show 方法负责将输出回显到视图脚本。
我建议您在为数组赋值后在 load 方法中调用它,如下所示:
<?php
public function load($view,$data =[]){
extract($data);
$viewFile = PATH_APPLICATION . '/views/' . $view . '.php';
if(file_exists($viewFile)){
ob_start();
require_once $viewFile;
$content = ob_get_clean();
$this->loadedContent[] = $content; //Push the contents of the Buffer to the internal $loadedContent Array
}
$this->Show(); // You should call the show method to echo the html...
}
public function Show() {
foreach($this->loadedContent as $html) {
echo $html;
}
}
然后回到视图中,您可以尝试以下操作:
< div id ="main-Body">
<?php $this->load($content);?>
<!-- THIS SHOULD DO THE TRICK. -->
</div>
我希望你觉得这有帮助...祝你好运和欢呼...