0

Block code(path: \magento\app\code\Sample\HelloWorld\Block\Index.php)

<?php 
namespace Sample\HelloWorld\Block;
use \Magento\Framework\View\Element\Template;
use Sample\HelloWorld\Model\PostFactory;

class Index extends Template {
    protected $_postFactory;
    public function __construct(
        Template\Context $context,
        PostFactory $postFactory
    ) {
        parent::__construct($context);
        $this->_postFactory = $postFactory;
    }
 
    public function getData()
    {
        $post = $this->_postFactory->create();
        $collection = $post->getCollection();
        return $collection;
    }
} 

Template file code (path: \magento\app\code\Sample\HelloWorld\view\frontend\templates\index.phtml)

<?php
/**
 * @var \Sample\HelloWorld\Block\Display $block
 */
?>
<h2>Hello <b>World</b></h2>

<table>
    <tr>
        <th><?= __('Post ID'); ?></th>
        <th><?= __('Name'); ?></th>
        <th><?= __('URL Key'); ?></th>
        <th><?= __('Post Contents'); ?></th>
        <th><?= __('Tags'); ?></th>
        <th><?= __('Status'); ?></th>
        <th><?= __('Updated At'); ?></th>
        <th colspan="2"><?= __('Action'); ?></th>
    </tr>
    <?php
        $collectionData = $block->getData();
        echo($collectionData);
        foreach($collectionData as $collection):
            print_r($collection);
        endforeach;
    ?>
    
    
</table>

Table structure:

enter image description here

Output is empty page. When I remove all code inside the Index class and just keep a hello world in the template, The hello world text is showing successfully.

Please help, I can't find the issue in this..

2
  • Can you take the guide from this reference link which shows steps wise process for what you want. mageplaza.com/magento-2-module-development/… Feb 7, 2022 at 12:56
  • Thanks, It is working in that way. So we need to write code in controller.. Not in block? Also, If I want to pass the collection to the template, How can I do that..
    – Thulya
    Feb 7, 2022 at 13:25

2 Answers 2

0

you are calling data from collection factory so,if you want to print data than you update your code by me code(below).

  $collectionData = $block->getData();
    foreach($collectionData as $collection):
        print_r($collection->getData());
    endforeach;
1
  • Tried but my template file is not showing any output
    – Thulya
    Feb 8, 2022 at 8:37
0

You can write function in block class file for fetching a collection and call that function in template file are as below example.

1] Block Class file

namespace vendorNanme\ModuleName\Block\Index;

use Magento\Framework\App\RequestInterface;
use vendorName\ModuleName\Model\Hello;

class Hello extends \Magento\Framework\View\Element\Template 
{
    protected $helloModelFactory;
    protected $request;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        Hello $helloModelFactory,
        RequestInterface $request,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->_helloModelFactory = $helloModelFactory;
        $this->_request = $request;
    }

    /**
     * Preparing global layout
     *
     * @return $this
     */
    protected function _prepareLayout() {
        parent::_prepareLayout();
        $this->pageConfig->getTitle()->set(__('Display Collection'));
        return $this;
    }

    public function getHello()
    {
        $collection = $this->_helloModelFactory->getCollection();
        return $collection;
    }
}

2] Template file (index.phtml)

<?php
//This line call the block class get hello function.
var_dump($block->getgetHello()->getData());

For more details you can click me

2
  • Thanks for your reply. But now I just deleted everything from my template file and just added a php echo , nothing is writing in the frontend. I tried echo inside controller, it is working. Where will be the error? I checked layout, block everything.
    – Thulya
    Feb 8, 2022 at 8:13
  • Verify your code with this reference link step-wise. mageplaza.com/magento-2-module-development/… Feb 8, 2022 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.