在内容块中不能使用两次phtml模板

时间:2015-02-08 22:28:38

标签: magento

我想设置一个像这样的Magento页面:

<h2>Title</h2>

{{block type="catalog/navigation" category_id="5" template="/catalog/navigation/subcategorylisting-distinctive.phtml"}}
<h2>Title</h2>

{{block type="catalog/navigation" category_id="9" template="/catalog/navigation/subcategorylisting-distinctive.phtml"}}
<h2>Title</h2>

{{block type="catalog/navigation" category_id="38" template="/catalog/navigation/subcategorylisting-distinctive.phtml"}}

我的问题是,我呼叫的模板只能在网站中使用一次。如果我第二次打电话,我会得到一个空白页。

<div id="categories">

    <div class="col_full">

        <div class="products-grid row first last odd" >

            <?php

            function getImageUrl($category)

            {

                $cur_category=Mage::getModel('catalog/category')->load($category->getId());

                $layer = Mage::getSingleton('catalog/layer');

                $layer->setCurrentCategory($cur_category);

                $url = $this->getCurrentCategory()->getImageUrl();

                return $url;

            };


            //Get the Current Category
            $category = $this->getData('category_id');
            $_maincategorylisting=Mage::getModel('catalog/category')->getCategories($category);


            // Iterate all categories in store

            foreach ($_maincategorylisting as $_category):


                // If category is Active

                if($_category->getIsActive()):


                    // Load the actual category object for this category

                    $cur_category = Mage::getModel('catalog/category')->load($_category->getId());


                    // Load a random product from this category

                    $products = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cur_category);

                    $products->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(100);


                    $products->load();


                    // This a bit of a fudge - there's only one element in the collection

                    $_product = null;


                    foreach ( $products as $_product ) {}


                    if(isset($_product)):

                        ?>


                        <div class="col-xs-12 col-sm-4 col-md-3 col-lg-3" >

                            <div class="img-responsive"><p><a href="<?php echo $this->getCategoryUrl($_category)?>" class="product-image">


                                        <?php

                                        $layer = Mage::getSingleton('catalog/layer');

                                        $layer->setCurrentCategory($cur_category);

                                        ?>


                                        <?

                                        // If there is an image set for the category - Display it

                                        if($_imgUrl=$this->getCurrentCategory()->getImageUrl()):?>

                                            <img src="<?php echo $_imgUrl ?>" class="img-responsive" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />

                                        <?php endif; ?>


                                        <?

                                        // If there is not a image set for that Category - Display a random product Image

                                        if(!$_imgUrl):


                                            // Let's load the category Model and grab the product collection of that category


                                            $product_collection = Mage::getModel('catalog/category')->load($_category->getId())->getProductCollection();

                                            $product_collection->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(1);


                                            // Now let's loop through the product collection and print the ID of every product

                                            foreach($product_collection as $product) {


                                                // Get the product ID

                                                $product_id = $product->getId();


                                                // Load the full product model based on the product ID


                                                $full_product = Mage::getModel('catalog/product')->load($product_id);


                                                // Now that we loaded the full product model, let's access all of it's data


                                                // Let's get the Product Name


                                                $product_name = $full_product->getName();


                                                // Let's get the Product URL path


                                                $product_url = $full_product->getProductUrl();


                                                // Let's get the Product Image URL


                                                $product_image_url = $full_product->getImageUrl();


                                                // Let's print the product information we gathered and continue onto the next one

                                            } //End For Each

                                            ?>

                                            <img src="<?php echo $product_image_url; ?>" class="img-responsive" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />

                                        <?php endif; ?>

                                    </a>

                            </div>

                            <a href="<?php echo $this->getCategoryUrl($_category)?>">

                                <h2 class="text-center product-name" ><?php echo $_category->getName()?></a></h2>

                            <? if($_description=$this->getCurrentCategory()->getDescription()):?>

                                <p class="category-description">

                                    <?php // echo $_description ?></p>

                            <?php   endif ?>

                        </div>

                    <?php

                    endif;

                endif;

            endforeach;

            ?>

        </div>

        <br clear=all>

    </div>

</div>

有人看到我做错了什么吗?我是否必须关闭一些我失踪的论点?感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您在模板中定义了一个功能。一个函数只能定义一次。

做得对吗

不要使用功能。 Magento的方法是定义你自己的扩展Mage_Catalog_Block_Navigation的块并将该函数作为方法添加到该块或将函数移动到帮助器类。

快速解决方法

将函数创建为闭包而不是正常的全局函数:

$getImageUrl = function($category) {
    // ...
}

并将其称为:

$getImageUrl($category)