magento在cms页面添加自定义的模板标签

比如要在page模板里面实现这样一个标签

{ {extraimage index=”1”}}

目的是显示当前page_id下面,index字段为1的所有图片.

步骤:

1,定义config.xml

 

1
2
3
4
5
6
7
8
9
10
11
12
<global>
....
<cms>
<page>
<tempate_filter>topsport/template_filter</tempate_filter>
</page>
<block>
<tempate_filter>topsport/template_filter</tempate_filter>
</block>
</cms>
....
</global>

其中topsport为当前插件的model。

2,新建model。

Mage_Widget_Model_Template_Filter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
class Iggo_Topsport_Model_Template_Filter extends Mage_Widget_Model_Template_Filter
{
/**
* Generate tgeneral
*
* @param array $construction
* @return string
*/
public function extraimageDirective($construction)
{
if(is_null(Mage::getBlockSingleton('cms/page')->getPage())){
return $construction[0];
}

$pageId = Mage::getBlockSingleton('cms/page')->getPage()->getPageId();

$params = $this->_getIncludeParameters($construction[2]);

$allowedParams = array('index');

/*
* check if param is not empty
* or atleast one allowed param exists
*/
if(empty($params) || !count(array_intersect($allowedParams, array_keys($params)))){
return $construction[0];
}
if(isset($params['index']) && !empty($params['index'])){
return Mage::helper('topsport')->getExtraImage($params['index'],$pageId,'topsport/cmsExtraImages');
}
// if it still reaches here, send original text
return $construction[0];
}

3,help文件

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php 
class Iggo_Topsport_Helper_Data extends Mage_Core_Helper_Abstract{
public function getExtraImage($index, $id,$model){
if($model == 'topsport/cmsExtraImages'){
$filterId = 'page_id';
$imageClass = 'iggo_advancedcms_extra_images_img';
$aClass = 'iggo_advancedcms_extra_images_a';
}
$imgHtml = '';
$imagesModel = Mage::getModel($model)->getCollection();
$imagesModel->addFieldToFilter("main_table.{$filterId}",array('eq'=>$id));
$imagesModel->addFieldToFilter('main_table.index',array('eq'=>$index));
$imagesModel->getFirstItem();
if($imagesModel->getSize() >0){
foreach ($imagesModel as $_image){
$path = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) . $_image->getPath();
$imgHtml = "![]({$path})";
if($_image->getIsLink() == 1){
if($_image->getLinkType() == 'external'){
$href = $_image->getTargetUrl();
}elseif($_image->getLinkType() == 'internal'){
$href = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB) . $_image->getTargetUrl();
}
$openNew = '';
if($_image->getOpenNew() == 1){
$openNew = "target='_brank'";
}
$imgHtml = "[getTitle()}'>{$imgHtml}]({$href} "{$_image-")";
}
break;
}
}
return $imgHtml;
}
}

?>

坚持原创技术分享,您的支持将鼓励我继续创作!