How in Block


a productcollection of template



$productCollection = $this->categoryFactory->create()->getCollection()
              ->addAttributeToFilter('is_active', 1)
              ->setOrder('position', 'ASC')

$html = $produCtcollection->toHtml(); ?????

  • 1
    A product collection is just data, there is nothing to render. What are you trying to do? Display a product list like in category and search results? – Fabian Schmengler Jun 22 '16 at 10:24
  • Display a product list like in category – Andrej Wasemiller Jun 22 '16 at 10:54

toHtml() return block's html output



    public function toHtml()
/* dode */
            $html = $this->_toHtml();
/* dode */
        return $html;

Next this goes to \vendor\magento\framework\View\Element\Template.php

protected function _toHtml()
    if (!$this->getTemplate()) {
        return '';
    return $this->fetchView($this->getTemplateFile());

Renders block html. and $this->fetchView function retrieves the block view from file (template).

Assume you know templates are using block functions and blocks extends core classes ( Template.php or AbstractBlock.php not in all cases ).

The data is populating by using toHtml()

Hope this helps.

| improve this answer | |

maybe I asked wrong... This solution I was looking for:


 public function getBlockProductsHtml($collection) {
   $html = '';
   $html = $this->_layout
        ->setData('product_collection', $collection)
    return $html;



thanks to all

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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