Magento 1.9 – Calling Custom Block from CMS Page Displays Blank Page

blockscms-blockcustom blockmagento-1.9template

I've created a custom block, however when calling the block from cms page displays nothing. Even the content of cms page does not displat that is above the call to custom block

Here is How i'm calling the block

{{block type="brandproducts/brandproducts"  template="brandproducts/brandproducts.phtml"}}

Custom Block Code is As Follows
enter image description here

Block File

<?php
// app/code/local/Compx/Brandproducts/Block/Recentproducts.php
class Compx_Brandproducts_Block_Brandproducts extends Mage_Core_Block_Template {
  public function getBrandProducts() {
    // call model to fetch data
    $arr_products = array();
    $products = Mage::getModel("brandproducts/brandproducts")->getBrandProducts();

    foreach ($products as $product) {
      $arr_products[] = array(
        'id' => $product­>getId(),
        'name' => $product->getName(),
        'url' => $product->getProductUrl(),
      );
    }

    return $arr_products;
  }
}
?>

Model File

<?php
// app/code/local/Compx/Brandproducts/Model/Brandproducts.php
class Compx_Brandproducts_Model_Brandproducts extends Mage_Core_Model_Abstract {
  public function getBrandProducts() {
  $products = Mage::getModel("catalog/product")->getCollection();
                $products->addAttributeToSelect('*')
                //->setOrder('entity_id', 'DESC')
                ->setPageSize(5);
    return $products;

  }
}

?>

Config file

<!-- app/code/local/Compx/Brandproducts/etc/config.xml -->
<?xml version="1.0"?>
<config>
  <modules>
    <Compx_Brandproducts>
      <version>1.0</version>
    </Compx_Brandproducts>
  </modules>
  <global>
    <blocks>
      <brandproducts>
        <class>Compx_Brandproducts_Block</class>
      </brandproducts>
    </blocks>
    <models>
      <brandproducts>
        <class>Compx_Brandproducts_Model</class>
      </brandproducts>
    </models>
  </global>
</config>

This just outputs blank. If i remove the custom block placeholder from cms page, other static content loads fine

I've been calling direct template files from other pages and that just works fine, like {block type="core/template" .. } however not works at all with custom block

It just display nothing, not even print_r or echo strings

Template File

  <?php
// app/design/frontend/compx_package/compx/template/brandproducts/brandproducts.phtml


print_r("hi 123");

//exit;
$products = $this->getBrandProducts();
?>

<div id="product_list">
  <h1>Brand Products</h1>
  <?php if (is_array($products) && count($products)) { ?>
    <?php foreach($products as $product) { ?>
      <div>
        <a href="<?php echo $product['url'] ?>"><?php echo $product['name'] ?></a>
      </div>
    <?php } ?>
  <?php } ?>
</div>

xml file under app/etc directory

/app/etc/modules/Compx_Brandproducts.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Compx_Brandproducts>
            <active>true</active>
            <codePool>local</codePool>
        </Compx_Brandproducts>
    </modules>
</config>

Best Answer

Your block should extend from product list I believe.

<?php
// app/code/local/Compx/Brandproducts/Block/Recentproducts.php
class Compx_Brandproducts_Block_Brandproducts 
  extends Mage_Catalog_Block_Product_List{ 
  //extends Mage_Core_Block_Template {

  public function getBrandProducts() {
    // call model to fetch data
    $arr_products = array();
    $products = Mage::getModel("brandproducts/brandproducts")->getBrandProducts();

    foreach ($products as $product) {
      $arr_products[] = array(
        'id' => $product­>getId(),
        'name' => $product->getName(),
        'url' => $product->getProductUrl(),
      );
    }

    return $arr_products;
  }
}
?>

And your model file

<?php
// app/code/local/Compx/Brandproducts/Model/Brandproducts.php
class Compx_Brandproducts_Model_Brandproducts 
extends Mage_Catalog_Model_Abstract{
//extends Mage_Core_Model_Abstract {
  public function getBrandProducts() {
  $products = Mage::getModel("catalog/product")->getCollection();
                $products->addAttributeToSelect('*')
                //->setOrder('entity_id', 'DESC')
                ->setPageSize(5);
    return $products;

  }
}

?>
Related Topic