I'm following the exercises of "Magento2 development cookbook". In the part of "Adding a block of new products" (previously to that all works fine). I have:


<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <title>Helloworld Landingspage</title>
        <remove name="wishlist_sidebar" /> <!-- THIS DOESN'T WORKS -->
        <referenceContainer name="content">
            <block class="Packt\HelloWorld\Block\Landingspage" name="landingsblock" template="Packt_HelloWorld::landingspage.phtml" /> <!-- THIS WORKS -->
            <block class="Packt\HelloWorld\Block\Newproducts"  name="newproducts"  template="Packt_HelloWorld::newproducts.phtml" /><!-- THIS DOESN'T WORKS -->


namespace Packt\HelloWorld\Block;

use Magento\Framework\View\Element\Template;

class Newproducts extends Template
    private $_productCollectionFactory;
    protected $_logger;

    public function __construct(
        Template\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        array $data = [])
        parent::__construct($context, $data);
        $this->_logger = $context->getLogger();
        $this->_productCollectionFactory = $productCollectionFactory;

    public function getProducts() {
        $collection = $this->_productCollectionFactory->create();



    return $collection;



<h2>New Products</h2>

    <?php foreach ($block->getProducts() as $product): ?>
        <li><?php echo $product->getName() ?></li>
    <?php endforeach; ?>
  • This shows the title New Products? – Khoa TruongDinh Jan 11 '17 at 15:58
  • no, it's shows the content of landingspage.phtml but not the newproducts.phtml – Muribury Jan 11 '17 at 16:04
  • Did you clear Magento Cache, remove var/view_preprocessed? – Khoa TruongDinh Jan 11 '17 at 16:09

Your code should work if you change templates/newproducts.php from php to phtml file. I saw this is our mistake.

Clear Magento Cache, remove var/view_preprocessed. And try again.

  • @Muribury if my answer is helpful, you can accept it as the solution. – Khoa TruongDinh Jan 15 '17 at 6:31

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.