2012-06-04 31 views

回答

2

我不知道有一種方法可以輕鬆地從靜態塊中引用這些值。

相反,我會建議你創建並使用一個小部件(在我看來,Magento中使用最少的功能之一),它將提供一個更清潔和更可擴展的方式來實現這一點 - 雖然它確實需要更多的工作: )

請參見下面的代碼Magento的窗口小部件的完整(簡體)例子不正是你從靜態塊問:

應用程序的/ etc /模塊/ YourCompany_Categorywidget.xml

<config> 
    <modules> 
     <MyCompany_Categorywidget> 
      <active>true</active> 
      <codePool>community</codePool> 
     </MyCompany_Categorywidget> 
    </modules> 
</config> 

ap P /代碼/小區/ MyCompany的/ Categorywidget的/ etc/config.xml中

<?xml version="1.0"?> 

<config> 
    <modules> 
     <MyCompany_Categorywidget> 
      <version>1.0.0</version> 
     </MyCompany_Categorywidget> 
    </modules> 
    <global> 
     <blocks> 
      <categorywidget> 
       <class>MyCompany_Categorywidget_Block</class> 
      </categorywidget> 
     </blocks> 
     <helpers> 
      <categorywidget> 
       <class>MyCompany_Categorywidget_Helper</class> 
      </categorywidget> 
     </helpers> 
    </global> 
</config> 

應用程序/代碼/小區/ MyCompany的/ Categorywidget的/ etc/widget.xml

<?xml version="1.0"?> 

<widgets> 
    <category_widget type="categorywidget/catalog_category_widget_info" translate="name description" module="categorywidget"> 
     <name>Category Info Block</name> 
     <description>Category Info Block showing name, image etc</description> 
     <parameters> 
      <block_title translate="label"> 
       <required>1</required> 
       <visible>1</visible> 
       <label>Block Title</label> 
       <type>text</type> 
      </block_title> 
      <template> 
       <required>1</required> 
       <visible>1</visible> 
       <label>Template</label> 
       <type>select</type> 
       <value>categorywidget/info.phtml</value> 
       <values> 
        <default translate="label"> 
         <value>categorywidget/info.phtml</value> 
         <label>Category Widget Info Block - Default Template</label> 
        </default> 
        <!-- Add different temmplates here for different block positions --> 
       </values> 
      </template> 
      <category translate="label"> 
       <visible>1</visible> 
       <required>1</required> 
       <label>Category</label> 
       <type>label</type> 
       <helper_block> 
        <type>adminhtml/catalog_category_widget_chooser</type> 
        <data> 
         <button translate="open"> 
          <open>Select Category...</open> 
         </button> 
        </data> 
       </helper_block> 
       <sort_order>10</sort_order> 
      </category> 
     </parameters> 
    </category_widget> 
</widgets> 

應用程序/代碼/小區/ MyCompany的/ Categorywidget /助手/ Data.php

<?php 

class MyCompany_Categorywidget_Helper_Data extends Mage_Core_Helper_Abstract 
{} 

應用程序/代碼/社區/ MyCompany的/ Categorywidget /座/目錄/分類/空間/ info.php的

<?php 

class MyCompany_Categorywidget_Block_Catalog_Category_Widget_Info 
    extends MyCompany_Categorywidget_Block_Catalog_Category_Info 
     implements Mage_Widget_Block_Interface 
{ 
    protected function _prepareCategory() 
    { 
     $this->_validateCategory(); 

     $category = $this->_getData('category'); 
     if (false !== strpos($category, '/')) { 
      $category = explode('/', $category); 
      $this->setData('category', (int)end($category)); 
     } 
     return parent::_prepareCategory(); 
    } 
} 

應用程序/代碼/社區/ MyCompany的/ Categorywidget /座/目錄/分類/ info.php的

<?php 

class MyCompany_Categorywidget_Block_Catalog_Category_Info extends Mage_Core_Block_Template 
{ 
    protected $_category; 

    protected function _beforeToHtml() 
    { 
     $this->_category = $this->_prepareCategory(); 
     return parent::_beforeToHtml(); 
    } 

    protected function _prepareCategory() 
    { 
     $this->_validateCategory(); 
     return Mage::getModel('catalog/category')->load($this->_getData('category')); 
    } 

    protected function _validateCategory() 
    { 
     if (! $this->hasData('category')) { 
      throw new Exception('Category must be set for info block'); 
     } 
    } 

    public function getCategoryName() 
    { 
     return $this->_category->getName(); 
    } 

    public function getCategoryImage() 
    { 
     return $this->_category->getImageUrl(); 
    } 
} 

應用程序/設計/前端/基/默認/模板/ categorywidget/info.phtml

<?php 
    $_categoryName = $this->getCategoryName(); 
    $_categoryImage = $this->getCategoryImage(); 
?> 

<div class="categoryinfo_block block"> 
    <p><strong><?php echo $_categoryName ?></strong></p> 
    <img src="<?php echo $_categoryImage ?>" alt="<?php echo $_categoryName ?>" /> 
</div> 
+0

我按照這個教程 - 窗口小部件在哪裏出現? – TheBlackBenzKid

0

我想鏈接到類別的圖像,以及,所以我說...

應用程序/代碼/社區/ MyCompany的/ Categorywidget /座/目錄/分類/ info.php的

public function getCategoryUrl() 
{ 
    return $this->_category->getUrl(); 
} 

應用程序/設計/前端/基/默認/模板/ categorywidget/info.phtml

<?php 
$_categoryName = $this->getCategoryName(); 
$_categoryImage = $this->getCategoryImage(); 
$_categoryUrl = $this->getCategoryUrl(); 
?> 

<div class="categoryinfo_block block"> 
    <p><strong><?php echo $_categoryName ?></strong></p>  
    <a href="<?php echo $_categoryUrl ?>" title="<?php echo $_categoryName ?>" alt="<?php echo $_categoryName ?>"> 
     <img src="<?php echo $_categoryImage ?>" alt="<?php echo $_categoryName ?>" /> 
    </a> 
</div> 

如果幫助別人?

+0

如何獲得我正在使用的類別1.9的簡短說明? – TheBlackBenzKid

相關問題