TOP
首頁 > ECSHOP 相關文章與模板 > Ecshop 綜合文章 > ECSHOP 響應式模板使用PHP

ECSHOP 響應式模板使用PHP

2022-03-23 237

相關文章

ecshop 預設模板是無法使用PHP程式碼的,但是有時候因為某些情況需要用到PHP,例如從資料庫獲取資料,或是 assign 一些 Smarty 變數,此時除了可以直接寫入相應檔案之外,也可以將新增的PHP檔案放在模板目錄內,再 include 到 includes/lib_main.php 的 assign_template 函數最後,例如:
$main = "themes/".$GLOBALS['_CFG']['template']."/main.php";
	if(is_file($main)) include($main);	
然後在 main.php 就可以添加想要 assign 給模板的資料,例如:
$pname = basename($_SERVER['SCRIPT_NAME'],'.php');// 目前PHP頁面
$smarty->assign('pname', $pname);
$smarty->assign('article_categories',   article_categories_tree());// 全域文章分類
/* 首頁主廣告設置 */
$sql  = 'SELECT a.ad_id, a.position_id, a.media_type, a.ad_link, a.ad_code, a.ad_name, p.position_style, RAND() AS rnd FROM ' . $GLOBALS['ecs']->table('ad') . ' AS a LEFT JOIN ' . $GLOBALS['ecs']->table('ad_position') . ' AS p ON a.position_id = p.position_id WHERE enabled = 1 AND start_time <= "' . gmtime() . '" AND end_time >= "' . gmtime() . '" AND a.position_id = 1 ORDER BY rnd LIMIT 5';
$res = $GLOBALS['db']->GetAll($sql);
$ads = array();
$position_style = '';
foreach ($res AS $row)
{
$position_style = $row['position_style'];
if ($row['media_type'] == 0) {
	$src = (strpos($row['ad_code'], 'https://') === false && strpos($row['ad_code'], 'https://') === false) ? DATA_DIR . "/afficheimg/$row[ad_code]" : $row['ad_code'];
	$ads[] = '<a href="affiche.php?ad_id='.$row[ad_id].'&uri=' .urlencode($row["ad_link"]).'" target="_blank"><img src="'.$src.'" alt="' .$row['ad_name'].'"  /><div class="text">'.$row['ad_name'].'</div></a>';
}
}
$smarty->assign('ads', $ads);
/* 首頁主廣告設置 */
//QRcode
$qr_path = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$smarty->assign('qrcode', $qr_path);
//偵測瀏覽器
$agent = $_SERVER['HTTP_USER_AGENT'];
if(strpos($agent,"MSIE")){
	$smarty->assign('browse',    "IE");
}else if(strpos($agent,"Firefox")){
	$smarty->assign('browse',    "FF");
}else if(strpos($agent,"Chrome")){
	$smarty->assign('browse',    "GG");
}else if(strpos($agent,"Safari")){
	$smarty->assign('browse',    "SF");
}else if(strpos($agent,"Opera")){
	$smarty->assign('browse',    "OP");
}else{
	$smarty->assign('browse',    "DF");
}
點擊圖片可以放大檢視
ECSHOP 響應式模板使用PHP
如果你對ECSHOP 響應式模板使用PHP有任何問題請到討論區發帖。