[PHP] XOOPS 加入外部檔案驗證

by Mesak

XOOPS 寫模組有個問題….

假設模組需要用到 Jquery檔案,要載入

一般寫在 HEADER 下面就行了

$xoopsTpl->assign("xoops_module_header", '');

但是,XOOPS也是有佈景檔的,當佈景檔已經載入 JQUERY.JS 這時候就會發生小小衝突

寫了一個 函式,由PHP 判斷 ‘jquery/jquery.js’ 這隻程式是否被載入

下列 程式 寫入 模組公用函式 function.php 裡面 ,並在一開始先載入

function tpl_file_include($tpl_include){
if(!isset($xoopsTpl)){ $xoopsTpl = new XoopsTpl(); }
$xoopsTheme = $xoopsTpl->fetch($GLOBALS['xoTheme']->path . '/' . $GLOBALS['xoTheme']->canvasTemplate);
foreach($tpl_include as $type => $file_array){
foreach($file_array as $file){
if(strpos($xoopsTheme, $file) === FALSE){
switch($type){
case 'js':
$GLOBALS['xoTheme']->addScript(XOOPS_URL.'/browse.php?Frameworks/' . $file);
break;
case 'css':
$GLOBALS['xoTheme']->addStylesheet( XOOPS_URL.$file );
break;
case 'javascript':
$GLOBALS['xoTheme']->addScript(null,null,$file);
break;
}
}
}
}
}

在 HEADER 之前 先寫入 變數

$tpl_include['js'] = array('jquery/jquery.js','jquery/plugins/jquery-ui.js');

必須使用陣列 可以多檔案

在header 之後  直接引入 tpl_file_include($tpl_include);

前台後台 皆可以這樣用…

來個簡易範例(後台):

include 'header.php';
include '../include/function.php';
include_once XOOPS_ROOT_PATH . '/include/cp_header.php';
include_once XOOPS_ROOT_PATH . '/include/cp_functions.php';
$tpl_include['js'] = array('jquery/jquery.js','jquery/plugins/jquery-ui.js');
xoops_cp_header();
tpl_file_include($tpl_include);
xoops_cp_footer();

前台:

include_once '../../mainfile.php';
include_once 'include/function.php';
$tpl_include['js'] = array('jquery/jquery.js','jquery/plugins/jquery-ui.js');
include_once XOOPS_ROOT_PATH.'/header.php';
tpl_file_include($tpl_include);
include_once XOOPS_ROOT_PATH.'/footer.php';

You may also like