duanguan3863 2015-11-02 09:07
浏览 48
已采纳

Magento - 在块模板中未定义变量

I am making a simple magento block and a template for it.

In my custom module I have a config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <AAA_Recentproducts>
      <version>1.0</version>
    </AAA_Recentproducts>
  </modules>
  <global>
    <blocks>
      <recentproducts>
        <class>AAA_Recentproducts_Block</class>
      </recentproducts>
    </blocks>
  </global>
</config>

And this is my Block

<?php
class AAA_Recentproducts_Block_Recentproducts extends Mage_Core_Block_Template {
  public function getRecentProducts() {
    $products = Mage::getModel("catalog/product")
                    ->getCollection()
                    ->addAttributeToSelect('*')
                    ->setOrder('entity_id', 'DESC')
                    ->setPageSize(5);
    return $products;
  }
}

And finally my template file:

<?php
$products = $this­->getRecentProducts();
//$products = [];
?>

<div id="product_list">
  <h1>Recent Products</h1>
  <?php if (is_array($products) && count($products)) { ?>
    <?php foreach($products as $product) { ?>
      <div>
    product
      </div>
    <?php } ?>
  <?php } ?>
</div>

The error I get is: 2015-11-02T08:53:55+00:00 ERR (3): Notice: Undefined variable: this­ in /var/www/html/magento/app/design/frontend/smartwave/granada/template/recentproducts/recentproducts.phtml on line 2

Does anyone have an idea why this error occurs?

  • 写回答

3条回答 默认 最新

  • dongshuobei1037 2015-11-02 09:12
    关注

    You can't use $this if you're not inside a class. You have to create an instance of AAA_Recentproducts_Block_Recentproducts and call the function on that object.

    $object = new AAA_Recentproducts_Block_Recentproducts();
    $products = $object->getRecentProducts();
    

    I wouldn't recommend the variable name $object, but i can't think of a better one right now. You get the point.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站