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(); ?????
-
1A 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– Fabian Schmengler2016年06月22日 10:24:23 +00:00Commented Jun 22, 2016 at 10:24
-
Display a product list like in categoryAndrej Wasemiller– Andrej Wasemiller2016年06月22日 10:54:56 +00:00Commented Jun 22, 2016 at 10:54
2 Answers 2
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.
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