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:
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..