2012-06-04 29 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

相关问题