AnswerBun.com

How in Block a productcollection of template render?

Magento Asked by Andrej Wasemiller on December 24, 2020

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(); ?????

2 Answers

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

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

 public function getBlockProductsHtml($collection) {
   $html = '';
   $html = $this->_layout
        ->createBlock('MagentoCatalogWidgetBlockProductProductsList')
        ->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

Answered by Andrej Wasemiller on December 24, 2020

toHtml() return block's html output

Process

vendormagentoframeworkViewElementAbstractBlock.php

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

Next this goes to vendormagentoframeworkViewElementTemplate.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.

Answered by Krishna ijjada on December 24, 2020

Add your own answers!

Related Questions

Send email to a friend is not working

2  Asked on December 26, 2020 by bineesh

     

How in Block a productcollection of template render?

2  Asked on December 24, 2020 by andrej-wasemiller

     

Magento 2 Sort order on catalogue widget

0  Asked on December 23, 2020 by carter

   

MAC OS Magento 2.4.1. PWA Studio 8.0.0 Local Setup Steps and Bugfix

0  Asked on December 19, 2020 by rishimukesh

   

Overriding Helper

2  Asked on December 19, 2020 by myron

   

Magento emails are not going

1  Asked on December 18, 2020 by user00247

   

Problem with payment,

1  Asked on December 16, 2020 by mariam-todadze

     

Special Price Not Applying in Magento2 Cart

2  Asked on December 14, 2020 by dinesh

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP