0

How in Block

app/code/Name/Name/Block/Rewrite/TopMyMenu.php

a productcollection of template

"vendor/magento/module-catalog-widget/view/frontend/templates/product/widget/content/grid.phtml"

render?

$productCollection = $this->categoryFactory->create()->getCollection()
              ->addAttributeToSelect('*')
              ->addAttributeToFilter('is_active', 1)
              ->setOrder('position', 'ASC')
              ->addIdFilter($category->getChildren());

$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
0

toHtml() return block's html output

Process

\vendor\magento\framework\View\Element\AbstractBlock.php

    public function toHtml()
    {
/* dode */
            $this->_beforeToHtml();
            $html = $this->_toHtml();
            $this->_saveCache($html);
/* 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.

0

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

...
getBlockProductsHtml($productCollection);
...

 public function getBlockProductsHtml($collection) {
   $html = '';
   $html = $this->_layout
        ->createBlock('Magento\CatalogWidget\Block\Product\ProductsList')
        ->setTemplate('Namespace_Module::html/products-grid.phtml')
        ->setData('product_collection', $collection)
        ->toHtml();
    return $html;
 }

or

->setTemplate('Magento_CatalogWidget::product/widget/content/grid.phtml')

thanks to all

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.