2017-02-28 89 views
0

请建议我如何在php布局模板中添加外部css/js文件。 我现在的代码附在下面。PHP在布局模板中添加外部css和js文件

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); 
    } 
} 

这里是footer.php文件

</body> 
</html> 
%__script__% 

此功能在每一页精细呼叫页眉和页脚功能。 但我想保持这样的

我的布局文件

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

我想在布局文件的HTML输出

*** Update *** 

脚本的末尾添加外部的js文件中使用的定义单个布局文件内容页面(内容页面)中的对象,如$ temp-> script_src =“js文件名”。如果script_src发现,那么它会在布局文件(layout.php中)在HTML文件的末尾

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

回答

0

你的问题是有点不清楚我加载,但如果你想添加一个外部的js文件中添加你的html输出结束,试试这样的:

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); 
}); 
+0

感谢您的回应。脚本使用内容页面(内容页面)中的对象来定义,如$ temp-> script_src =“js文件名称”。如果找到script_src,那么它将在HTML文件的末尾加载布局文件(layout.php)。 – Vam

相关问题