0

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.

10
  • < div space is really exist or it just copy paste issue ? Commented Apr 28, 2016 at 7:13
  • 1
    Where do you echo or print the output? Commented Apr 28, 2016 at 7:14
  • What is in your view files? Commented Apr 28, 2016 at 7:15
  • 1
    I think your file my html extention is .html and .htm this is reason that php tag converts itno comments Commented Apr 28, 2016 at 7:15
  • i just edited my question, only HTML code in view file. Commented Apr 28, 2016 at 7:20

2 Answers 2

1

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;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.