ajax很简单。自然,它也可以很复杂得不近人情。
我做的网站首页有个模块是从别的网站捞天气。缓存之本地。每天第一次请求时获取。
常常在这里会让整个页面停顿,给人以这个网站很慢的感觉。所以我想到了用ajax异步获取。
mambo 4.5.2装载模块的部分是在/includes目录下的frontend.php里。
我修改了下,新增了几个函数,用来装载ajax模块,并将模块盒子的输出改为模板控制。
这下感觉好多了。

 /* 客户端模块内容的外壳,id明确=模块名。由于模块名独立,所以不会产生冲突 */
/* 客户端模块内容的外壳,id明确=模块名。由于模块名独立,所以不会产生冲突 */
 function ajaxClient($name,$loading,$styleObj=''){
function ajaxClient($name,$loading,$styleObj=''){
 $style = "style=\"$styleObj\"";
    $style = "style=\"$styleObj\"";
 $client = "<div id='$name' $style>$loading</div>\n";
    $client = "<div id='$name' $style>$loading</div>\n";
 $client .= ajaxClientJs($name);
    $client .= ajaxClientJs($name);
 return $client;
    return $client;
 }
}

 /* 生成客户端请求的脚本其中oXML对象在ajax.js中就已经定义了 */
/* 生成客户端请求的脚本其中oXML对象在ajax.js中就已经定义了 */
 function ajaxClientJs($name,$clientFunc=''){
function ajaxClientJs($name,$clientFunc=''){
 global $mosConfig_live_site;
global $mosConfig_live_site;
 $clientFunc = $clientFunc?$clientFunc:"Fill_".$name;
$clientFunc = $clientFunc?$clientFunc:"Fill_".$name;
 //$site = "http://corp.cnnavi.com";
//$site = "http://corp.cnnavi.com";
 $url = $mosConfig_live_site."/modules/".$name.".php";
$url = $mosConfig_live_site."/modules/".$name.".php";
 $js=<<<EOD
$js=<<<EOD

 <script language="javascript" type="text/javascript">
<script language="javascript" type="text/javascript">
 <!--
<!--
 url = '$url';
url = '$url';
 oXML.LoadUrl( url,$clientFunc ,'');
oXML.LoadUrl( url,$clientFunc ,'');
 function $clientFunc(htmlstr){
function $clientFunc(htmlstr){
 var obj = document.getElementById('$name');
    var obj = document.getElementById('$name');
 obj.innerHTML = htmlstr;
    obj.innerHTML = htmlstr;
 }
}
 //-->
//-->
 </script>
</script>

 EOD;
EOD;
 return $js;
return $js;
 //send( urlToCall , params , func ,funcParam )
//send( urlToCall , params , func ,funcParam )
 }//end func
}//end func

 /* 异步装载模块数据调用 */
/* 异步装载模块数据调用 */
 function ajaxModuleCall($modName,$style='',$params="",$loading="loading
function ajaxModuleCall($modName,$style='',$params="",$loading="loading ..",$styleObj='')
..",$styleObj='')
 {
{    
 global $mosConfig_gzip, $mosConfig_absolute_path, $database, $my, $Itemid, $mosConfig_caching;
    global $mosConfig_gzip, $mosConfig_absolute_path, $database, $my, $Itemid, $mosConfig_caching;
 $client = ajaxClient($modName,$loading,$styleObj);
    $client = ajaxClient($modName,$loading,$styleObj);
 $contents = &$client;
    $contents = &$client;
 $module = $mosConfig_absolute_path .'/modules/'. $modName .'.php' ;
    $module = $mosConfig_absolute_path .'/modules/'. $modName .'.php' ;
 if (!file_exists ($module)) {
    if (!file_exists ($module)) {
 $msg = "module  file {$module} not exists, please check";
        $msg = "module  file {$module} not exists, please check";
 return $msg;
        return $msg;
 }
    }
 //取模块参数
    //取模块参数
 $q = "select title,showtitle,params from #__modules where module='$modName'  ";
    $q = "select title,showtitle,params from #__modules where module='$modName'  ";
 $database->setQuery($q);
    $database->setQuery($q);    
 $database->loadObject($rowMod);//prt($rowMod);
    $database->loadObject($rowMod);//prt($rowMod);
 $moduleParams = $params?$params:$rowMod->params;
    $moduleParams = $params?$params:$rowMod->params;
 $params = new mosParameters( $moduleParams );
    $params = new mosParameters( $moduleParams );
 $moduleclass_sfx = $params->get('moduleclass_sfx');
    $moduleclass_sfx = $params->get('moduleclass_sfx');
 $returnString = "";
    $returnString = "";
 $tplfile = $mosConfig_absolute_path."/templates/modulebox/box".$style.".html";
    $tplfile = $mosConfig_absolute_path."/templates/modulebox/box".$style.".html";
 if (!is_file($tplfile)) {
    if (!is_file($tplfile)) {
 $tplfile = $mosConfig_absolute_path."/templates/modulebox/box.html";
        $tplfile = $mosConfig_absolute_path."/templates/modulebox/box.html";
 }
    }
 $boxTemplate = file_get_contents ( $tplfile );
    $boxTemplate = file_get_contents ( $tplfile );
 /* 是否输出头 */
    /* 是否输出头 */
 if ($rowMod->showtitle==1)
    if ($rowMod->showtitle==1) 
 $rowModTitle = $rowMod->title;
        $rowModTitle = $rowMod->title;
 else
    else 
 $boxTemplate = preg_replace("#.*{$rowModTitle}.*#","",$boxTemplate);
        $boxTemplate = preg_replace("#.*{$rowModTitle}.*#","",$boxTemplate);
 
    
 /* 替换模版 */
    /* 替换模版 */
 $htmldata  = preg_replace("#{([^}]*)}#e","\\1",$boxTemplate);
    $htmldata  = preg_replace("#{([^}]*)}#e","\\1",$boxTemplate);
 return $htmldata ;
    return $htmldata ;
 } // end func
} // end func



 /**
/**
 * @param string The modName
* @param string The modName
 * @param int The style.  0=normal, 1=horiz, -1=no wrapper
* @param int The style.  0=normal, 1=horiz, -1=no wrapper
 * 本模块调用返回字符串,所以,在执行模块里不可有任何输出缓冲。
* 本模块调用返回字符串,所以,在执行模块里不可有任何输出缓冲。
 */
*/
 function mosLoadModule2( $modName='', $style=0 , $params='' ) {
function mosLoadModule2( $modName='', $style=0 , $params='' ) {
 global $mosConfig_gzip, $mosConfig_absolute_path, $database, $my, $Itemid, $mosConfig_caching;
    global $mosConfig_gzip, $mosConfig_absolute_path, $database, $my, $Itemid, $mosConfig_caching;
 $module = $mosConfig_absolute_path .'/modules/'. $modName .'.php' ;
    $module = $mosConfig_absolute_path .'/modules/'. $modName .'.php' ;
 if (!file_exists ($module)) {
    if (!file_exists ($module)) {
 $msg = "module  file {$module} not exists, please check";
        $msg = "module  file {$module} not exists, please check";
 return $msg;
        return $msg;
 }
    }

 //取模块参数
    //取模块参数
 $q = "select title,showtitle,params from #__modules where module='$modName'  ";
    $q = "select title,showtitle,params from #__modules where module='$modName'  ";
 $database->setQuery($q);
    $database->setQuery($q);    
 $database->loadObject($rowMod);//prt($rowMod);
    $database->loadObject($rowMod);//prt($rowMod);
 $moduleParams = $params?$params:$rowMod->params;
    $moduleParams = $params?$params:$rowMod->params;
 $params = new mosParameters( $moduleParams );
    $params = new mosParameters( $moduleParams );
 $moduleclass_sfx = $params->get('moduleclass_sfx');
    $moduleclass_sfx = $params->get('moduleclass_sfx');
 //执行模块
    //执行模块
 ob_start();
    ob_start();
 include( $module );
    include( $module );
 $contents = ob_get_contents();
    $contents = ob_get_contents();
 ob_end_clean();
    ob_end_clean();

 $tplfile = $mosConfig_absolute_path."/templates/modulebox/box".$style.".html";
    $tplfile = $mosConfig_absolute_path."/templates/modulebox/box".$style.".html";
 if (!is_file($tplfile)) {
    if (!is_file($tplfile)) {
 $tplfile = $mosConfig_absolute_path."/templates/modulebox/box.html";
        $tplfile = $mosConfig_absolute_path."/templates/modulebox/box.html";
 }
    }
 $boxTemplate = file_get_contents ( $tplfile );
    $boxTemplate = file_get_contents ( $tplfile );
 /* 是否输出头 */
    /* 是否输出头 */
 if ($rowMod->showtitle==1) {
    if ($rowMod->showtitle==1) {
 $rowModTitle = $rowMod->title;
        $rowModTitle = $rowMod->title;
 }
    }
 else {//如果不是,则将头部包含的一行html去掉。
    else {//如果不是,则将头部包含的一行html去掉。        
 $boxTemplate = preg_replace("#.*{$rowModTitle}.*#","",$boxTemplate);
        $boxTemplate = preg_replace("#.*{$rowModTitle}.*#","",$boxTemplate);
 }
    }
 $htmldata  = preg_replace("#{([^}]*)}#e","\\1",$boxTemplate);
    $htmldata  = preg_replace("#{([^}]*)}#e","\\1",$boxTemplate);
 return $htmldata ;
    return $htmldata ;
 }
}

我将模板放在templates/modulebox目录下。这下盒子的输出怎么变都简单了。新建文件的问题。而且保留原有的类别后缀。ok.
前台调用。
 //右边
//右边
 //function ajaxModuleCall($modName,$style=-1,$params="",$loading="loading
//function ajaxModuleCall($modName,$style=-1,$params="",$loading="loading ..",$styleObj='')
..",$styleObj='')

 //外汇
//外汇
 $waihui = ajaxModuleCall ( 'mod_getwaihui','',"","loading waihui
$waihui = ajaxModuleCall ( 'mod_getwaihui','',"","loading waihui
 ." );
." ); 

 //天气
//天气

 $styleObj = "height:250px;";
$styleObj = "height:250px;";
 $weather = ajaxModuleCall ( 'mod_weather','',"","loading weather
$weather = ajaxModuleCall ( 'mod_weather','',"","loading weather
 .", $styleObj);
.", $styleObj); 
 //echot($weather);
//echot($weather);

ok。你可以发现我将所有的输出都放在字符串里。这样做是为了模板的替换。
模板在这里得到简单的运用。而且有效。为什么不用smarty?phplib?
因为这样简单。而且我反正是要做html缓存。用不着smarty............
我做的网站首页有个模块是从别的网站捞天气。缓存之本地。每天第一次请求时获取。
常常在这里会让整个页面停顿,给人以这个网站很慢的感觉。所以我想到了用ajax异步获取。
mambo 4.5.2装载模块的部分是在/includes目录下的frontend.php里。
我修改了下,新增了几个函数,用来装载ajax模块,并将模块盒子的输出改为模板控制。
这下感觉好多了。

 /* 客户端模块内容的外壳,id明确=模块名。由于模块名独立,所以不会产生冲突 */
/* 客户端模块内容的外壳,id明确=模块名。由于模块名独立,所以不会产生冲突 */ function ajaxClient($name,$loading,$styleObj=''){
function ajaxClient($name,$loading,$styleObj=''){ $style = "style=\"$styleObj\"";
    $style = "style=\"$styleObj\""; $client = "<div id='$name' $style>$loading</div>\n";
    $client = "<div id='$name' $style>$loading</div>\n"; $client .= ajaxClientJs($name);
    $client .= ajaxClientJs($name); return $client;
    return $client; }
}
 /* 生成客户端请求的脚本其中oXML对象在ajax.js中就已经定义了 */
/* 生成客户端请求的脚本其中oXML对象在ajax.js中就已经定义了 */ function ajaxClientJs($name,$clientFunc=''){
function ajaxClientJs($name,$clientFunc=''){ global $mosConfig_live_site;
global $mosConfig_live_site; $clientFunc = $clientFunc?$clientFunc:"Fill_".$name;
$clientFunc = $clientFunc?$clientFunc:"Fill_".$name; //$site = "http://corp.cnnavi.com";
//$site = "http://corp.cnnavi.com"; $url = $mosConfig_live_site."/modules/".$name.".php";
$url = $mosConfig_live_site."/modules/".$name.".php"; $js=<<<EOD
$js=<<<EOD
 <script language="javascript" type="text/javascript">
<script language="javascript" type="text/javascript"> <!--
<!-- url = '$url';
url = '$url'; oXML.LoadUrl( url,$clientFunc ,'');
oXML.LoadUrl( url,$clientFunc ,''); function $clientFunc(htmlstr){
function $clientFunc(htmlstr){ var obj = document.getElementById('$name');
    var obj = document.getElementById('$name'); obj.innerHTML = htmlstr;
    obj.innerHTML = htmlstr; }
} //-->
//--> </script>
</script>
 EOD;
EOD; return $js;
return $js; //send( urlToCall , params , func ,funcParam )
//send( urlToCall , params , func ,funcParam ) }//end func
}//end func
 /* 异步装载模块数据调用 */
/* 异步装载模块数据调用 */ function ajaxModuleCall($modName,$style='',$params="",$loading="loading
function ajaxModuleCall($modName,$style='',$params="",$loading="loading ..",$styleObj='')
..",$styleObj='') {
{     global $mosConfig_gzip, $mosConfig_absolute_path, $database, $my, $Itemid, $mosConfig_caching;
    global $mosConfig_gzip, $mosConfig_absolute_path, $database, $my, $Itemid, $mosConfig_caching; $client = ajaxClient($modName,$loading,$styleObj);
    $client = ajaxClient($modName,$loading,$styleObj); $contents = &$client;
    $contents = &$client; $module = $mosConfig_absolute_path .'/modules/'. $modName .'.php' ;
    $module = $mosConfig_absolute_path .'/modules/'. $modName .'.php' ; if (!file_exists ($module)) {
    if (!file_exists ($module)) { $msg = "module  file {$module} not exists, please check";
        $msg = "module  file {$module} not exists, please check"; return $msg;
        return $msg; }
    } //取模块参数
    //取模块参数 $q = "select title,showtitle,params from #__modules where module='$modName'  ";
    $q = "select title,showtitle,params from #__modules where module='$modName'  "; $database->setQuery($q);
    $database->setQuery($q);     $database->loadObject($rowMod);//prt($rowMod);
    $database->loadObject($rowMod);//prt($rowMod); $moduleParams = $params?$params:$rowMod->params;
    $moduleParams = $params?$params:$rowMod->params; $params = new mosParameters( $moduleParams );
    $params = new mosParameters( $moduleParams ); $moduleclass_sfx = $params->get('moduleclass_sfx');
    $moduleclass_sfx = $params->get('moduleclass_sfx'); $returnString = "";
    $returnString = ""; $tplfile = $mosConfig_absolute_path."/templates/modulebox/box".$style.".html";
    $tplfile = $mosConfig_absolute_path."/templates/modulebox/box".$style.".html"; if (!is_file($tplfile)) {
    if (!is_file($tplfile)) { $tplfile = $mosConfig_absolute_path."/templates/modulebox/box.html";
        $tplfile = $mosConfig_absolute_path."/templates/modulebox/box.html"; }
    } $boxTemplate = file_get_contents ( $tplfile );
    $boxTemplate = file_get_contents ( $tplfile ); /* 是否输出头 */
    /* 是否输出头 */ if ($rowMod->showtitle==1)
    if ($rowMod->showtitle==1)  $rowModTitle = $rowMod->title;
        $rowModTitle = $rowMod->title; else
    else  $boxTemplate = preg_replace("#.*{$rowModTitle}.*#","",$boxTemplate);
        $boxTemplate = preg_replace("#.*{$rowModTitle}.*#","",$boxTemplate); 
     /* 替换模版 */
    /* 替换模版 */ $htmldata  = preg_replace("#{([^}]*)}#e","\\1",$boxTemplate);
    $htmldata  = preg_replace("#{([^}]*)}#e","\\1",$boxTemplate); return $htmldata ;
    return $htmldata ; } // end func
} // end func


 /**
/** * @param string The modName
* @param string The modName * @param int The style.  0=normal, 1=horiz, -1=no wrapper
* @param int The style.  0=normal, 1=horiz, -1=no wrapper * 本模块调用返回字符串,所以,在执行模块里不可有任何输出缓冲。
* 本模块调用返回字符串,所以,在执行模块里不可有任何输出缓冲。 */
*/ function mosLoadModule2( $modName='', $style=0 , $params='' ) {
function mosLoadModule2( $modName='', $style=0 , $params='' ) { global $mosConfig_gzip, $mosConfig_absolute_path, $database, $my, $Itemid, $mosConfig_caching;
    global $mosConfig_gzip, $mosConfig_absolute_path, $database, $my, $Itemid, $mosConfig_caching; $module = $mosConfig_absolute_path .'/modules/'. $modName .'.php' ;
    $module = $mosConfig_absolute_path .'/modules/'. $modName .'.php' ; if (!file_exists ($module)) {
    if (!file_exists ($module)) { $msg = "module  file {$module} not exists, please check";
        $msg = "module  file {$module} not exists, please check"; return $msg;
        return $msg; }
    }
 //取模块参数
    //取模块参数 $q = "select title,showtitle,params from #__modules where module='$modName'  ";
    $q = "select title,showtitle,params from #__modules where module='$modName'  "; $database->setQuery($q);
    $database->setQuery($q);     $database->loadObject($rowMod);//prt($rowMod);
    $database->loadObject($rowMod);//prt($rowMod); $moduleParams = $params?$params:$rowMod->params;
    $moduleParams = $params?$params:$rowMod->params; $params = new mosParameters( $moduleParams );
    $params = new mosParameters( $moduleParams ); $moduleclass_sfx = $params->get('moduleclass_sfx');
    $moduleclass_sfx = $params->get('moduleclass_sfx'); //执行模块
    //执行模块 ob_start();
    ob_start(); include( $module );
    include( $module ); $contents = ob_get_contents();
    $contents = ob_get_contents(); ob_end_clean();
    ob_end_clean();
 $tplfile = $mosConfig_absolute_path."/templates/modulebox/box".$style.".html";
    $tplfile = $mosConfig_absolute_path."/templates/modulebox/box".$style.".html"; if (!is_file($tplfile)) {
    if (!is_file($tplfile)) { $tplfile = $mosConfig_absolute_path."/templates/modulebox/box.html";
        $tplfile = $mosConfig_absolute_path."/templates/modulebox/box.html"; }
    } $boxTemplate = file_get_contents ( $tplfile );
    $boxTemplate = file_get_contents ( $tplfile ); /* 是否输出头 */
    /* 是否输出头 */ if ($rowMod->showtitle==1) {
    if ($rowMod->showtitle==1) { $rowModTitle = $rowMod->title;
        $rowModTitle = $rowMod->title; }
    } else {//如果不是,则将头部包含的一行html去掉。
    else {//如果不是,则将头部包含的一行html去掉。         $boxTemplate = preg_replace("#.*{$rowModTitle}.*#","",$boxTemplate);
        $boxTemplate = preg_replace("#.*{$rowModTitle}.*#","",$boxTemplate); }
    } $htmldata  = preg_replace("#{([^}]*)}#e","\\1",$boxTemplate);
    $htmldata  = preg_replace("#{([^}]*)}#e","\\1",$boxTemplate); return $htmldata ;
    return $htmldata ; }
}
我将模板放在templates/modulebox目录下。这下盒子的输出怎么变都简单了。新建文件的问题。而且保留原有的类别后缀。ok.
前台调用。
 //右边
//右边 //function ajaxModuleCall($modName,$style=-1,$params="",$loading="loading
//function ajaxModuleCall($modName,$style=-1,$params="",$loading="loading ..",$styleObj='')
..",$styleObj='')
 //外汇
//外汇 $waihui = ajaxModuleCall ( 'mod_getwaihui','',"","loading waihui
$waihui = ajaxModuleCall ( 'mod_getwaihui','',"","loading waihui
 ." );
." ); 
 //天气
//天气
 $styleObj = "height:250px;";
$styleObj = "height:250px;"; $weather = ajaxModuleCall ( 'mod_weather','',"","loading weather
$weather = ajaxModuleCall ( 'mod_weather','',"","loading weather
 .", $styleObj);
.", $styleObj);  //echot($weather);
//echot($weather);
ok。你可以发现我将所有的输出都放在字符串里。这样做是为了模板的替换。
模板在这里得到简单的运用。而且有效。为什么不用smarty?phplib?
因为这样简单。而且我反正是要做html缓存。用不着smarty............
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号