2014-10-08 39 views
2

我正在嘗試獲取模塊位置,以便可以根據它放置的位置來更改模板。我曾經從控制器內部檢查$setting['position'],然後將其傳遞給視圖,但由於模塊的定位是通過佈局管理器完成的,所以該屬性不再可用。在Opencart 2中查找模塊位置

這是Opencart的1.5.6什麼作品 -

// Module Controller 
$this->data['position'] = $setting['position']; 

// Module View 
<?php if ($position === "content_top" or $position === "content_bottom") { ?> 
    <!-- position specific markup --> 
<?php } ?> 

如何同樣的事情在Opencart的2可以實現嗎?

+0

我還沒有時間去應對OC 2,但是看看整個系統如何加載,實例化等等,並且可以說,如果沒有相當大的核心繫統修改,它看起來是不可能的......我可以告訴你更多或更精確的,我安裝後,並與它一起玩... – shadyyx 2014-10-08 13:09:46

+0

已經改變功能模塊位置在OC 2.所以,我認爲,你需要設置你的代碼根據OC 2.0。在OC 2中查看設置模塊位置 - http://www.harnishdesign.net/blog/2014/10/09/how-to-set-layout-position-of-module-in-opencart-2-0/ – HarnishDesign 2014-10-10 11:07:45

+0

@HarnishDesign解釋了模塊位置的設置,但它沒有說明從控制器中找到模塊位置 – mdgriffin 2014-10-10 15:36:41

回答

1

在以前,我們用它來設置position(content_top/content_bottom)模塊頁面(擴展/模塊)的特定模塊

現在Opencart的2.0,我們設置position(content_top/content_bottom),特別是佈局頁面(系統/設計/佈局)

模塊這就是您在模塊控制器中沒有$setting['position'];的原因。

如果您只想在期望的控制器中使用此代碼與期望的position(content_top/content_bottom)

$this->load->model('design/layout'); 

if (isset($this->request->get['route'])) { 
$route = (string)$this->request->get['route']; 
} else { 
$route = 'common/home'; 
} 

$layout_id = 0; 

if ($route == 'product/category' && isset($this->request->get['path'])) { 
$path = explode('_', (string)$this->request->get['path']); 

$layout_id = $this->model_catalog_category->getCategoryLayoutId(end($path)); 
} 

if ($route == 'product/product' && isset($this->request->get['product_id'])) { 
$layout_id = $this->model_catalog_product->getProductLayoutId($this->request->get['product_id']); 
} 

if ($route == 'information/information' && isset($this->request->get['information_id'])) { 
$layout_id = $this->model_catalog_information->getInformationLayoutId($this->request->get['information_id']); 
} 

if (!$layout_id) { 
$layout_id = $this->model_design_layout->getLayout($route); 
} 

if (!$layout_id) { 
$layout_id = $this->config->get('config_layout_id'); 
} 

$data['modules'] = array(); 

$modules = $this->model_design_layout->getLayoutModules($layout_id, 'content_top'); 

它會返回一個分配給content_top

+0

這將只返回在特定佈局和特定位置上的所有模塊的名稱,它不會說明在當前應用的模塊的哪個位置上。 – 2014-11-26 13:30:56

1

這是一種替代方法的模塊,我有同樣的問題,而轉換了我的一些模塊,我做到了與Opencart的2.工作與jQuery的客戶端。我JSON編碼我的模塊數據,並在tpl做了以下:

<div id="my_module_<?php echo $module; ?>"></div> 
<script type="text/javascript"> 

var data_json = '<?php echo $my_module_json; ?>'; 
var data = $.parseJSON(data_json); 
var target = $('#my_module_<?php echo $module; ?>'); 

var position = ''; 
if ($(target).closest('#column-left').length){ 
    position = 'column-left'; 
} 
else if ($(target).closest('#column-right').length){ 
    position = 'column-right'; 
} 
else{ 
    position = 'content'; 
} 

var html = ''; 

if (position == 'content') { 
    $html +='HTML for content position'; 
    // ... do whatever 
} 
else 
{ 
    $html +='HTML for column position'; 
    // ... do something else 
} 

$(target).html(html); 
</script> 

似乎工作正常。