1

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

Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
asked Jun 22, 2016 at 10:15
2
  • 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? Commented Jun 22, 2016 at 10:24
  • Display a product list like in category Commented Jun 22, 2016 at 10:54

2 Answers 2

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.

answered Jun 22, 2016 at 11:36
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

answered Jun 23, 2016 at 12:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.