PHP在布局模板中添加外部css和js文件

Please suggest me how to add external css/js file in php layout template. My current code is attached below.

Class Template{
  public function header() //it loads header.php file
  public function footer(){
     $footer = file_get_contents('footer.php');
      //this function load js file
      $search[] = "%__script__%";
      if(isset($this->script) && file_exists($this->script)){
        $replace = "<script src=\"$this->ready_script\">";
      }
      return str_replace($search, $replace, $header);
  }
}

Here is footer.php file

</body>
</html>
%__script__%

This function works fine call header and footer function in every page. But I want to maintain single layout file like this

My layout file

$temp = new Template();
echo $temp->header();
__CONTENT GOES HERE WHICH IS LOADED FROM AJAX__
echo $temp->footer();

I want add external js file at the end of layout file html output

*** Update ***

Script is defined using object in content page(content page) like $temp->script_src = "js file name" . if script_src found, then it will load in layout file (layout.php) at the end of html file

** My file structure **
App
  -- header
  -- content (in content external js file url defined by $temp->script property
  -- footer
  -- if $temp->script property found then load js using <script> tag

Your question is a bit unclear to me, but if you want to add an external js file add the end of your html output, try something like this:

document.addEventListener("DOMContentLoaded", function() {
    var bodyLoaded = document.body,
        url = "http://yourexternal.com/script.js",
        script = document.createElement('script');
    script.type = "text/javascript";
    script.src = url;    
    bodyLoaded.appendChild(script);
});