当购物商城发展到一定程度的时候,不仅仅是页面的增多体现,更多的是需要多个页面模板来衬托文章页面的个性化,比如当我们需要推广手机商品时,就需要做个跟手机一致的文章推广页面,而我们要推广水果商品时,可能就需要一个水果相一致的页面模板了,实现的方法也简单,下面夏日博客就来教你怎么做。
实现的方法我们可以以栏目分类ID来区分,比如栏目为水果的文章调用水果文章模板,栏目为手机商品的可以调用手机文章模板,也就是ecshop通过文章分类ID实现文章调用不同模板。
第一种方法,打开根目录下的文件 article_cat.php,查找如下代码:
1
|
$smarty
->display(
'article_cat.dwt'
,
$cache_id
);
|
这段代码是调用模板的关键,article_cat.dwt 是调用的模板文章了,而 $cache_id 就是文章栏目的分类ID了,这里没有指定分类ID,我们可以将这段代码修改如下:
1
2
3
4
5
6
7
8
|
switch
(
$cat_id
){
case
1:
//当文章分类ID等于1时,输出article_cat1.dwt模板。
$smarty
->display(
'article_cat1.dwt'
,
$cache_id
);
break
;
default
:
$smarty
->display(
'article_cat.dwt'
,
$cache_id
);
//当文章分类ID不等于1时,输出article_cat.dwt模板。
}
|
通过 PHP 的 switch 来指定分类ID,然后再调用不同的模板文件。
第二种方法,这种方法同上,将代码
1
|
$smarty
->display(
'article_cat.dwt'
,
$cache_id
);
|
修改如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
if
(
$cat_id
> 9)
{
$smarty
->display(
'article_cat1.dwt'
,
$cache_id
);
}
else
if
(
$cat_id
> 2 &&
$cat_id
<= 9)
{
$smarty
->display(
'article_cat2.dwt'
,
$cache_id
);
}
else
if
(
$cat_id
==1 ||
$cat_id
==2)
{
$smarty
->display(
'article_cat3.dwt'
,
$cache_id
);
}
|
意思是当栏目ID > 9 的时候,输出 article_cat1.dwt 模板,当栏目 ID > 2 和 <= 9 时,输出 article_cat2.dwt模板,当栏目ID=1 或者 ID =2 的时候,输出 arcticle_cat3.dwt 模板,其实两种方法都是一个原理,就是判断: [code lang="php"] $smarty->display('article_cat.dwt', $cache_id); [/code] 代码中的 $cache_id 的值而已,至于模板的话,自己在模板目录多建几个就可以了。