Reputation: 55
I run php code inside HTML code but it seems that php code loaded then HTML.
My HTML:
<div id ="main-Body">
<?php $this->load($content);?> //$content: link file
</div>
function 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;
}
public function Show()
{
foreach($this->loadedContent as $html)
{
echo $html;
}
}
A result on the Browser:
<!--It shows $this->load($content) here-->
<div id ="main-Body">
<!--nothing here-->
</div>
EDIT: if I put the following code into $content file
<div id ="main-Body">
</div>
Then show:
<?php $this->load($content);?> //$content: link file
It is nothing wrong.
Upvotes: 0
Views: 842
Reputation: 7617
I just saw the new updated question now.... Unless You are using a Framework which implicitly calls the Show Method, I'd say that You are not calling the Show method anywhere in your code... and from your code, the Show Method is responsible for echoing the output to the View Script.
I'd suggest you perhaps call it in the load method after assigning values to the the array like so:
<?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;
}
}
And then back in the View, you could try this:
< div id ="main-Body">
<?php $this->load($content);?>
<!-- THIS SHOULD DO THE TRICK. -->
</div>
I hope you find this helpful... Good luck and cheers...
Upvotes: 0
Reputation: 8101
Return the loadContent from function:
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;
}
Upvotes: 1