0

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:

app/code/Packt/HelloWorld/view/frontend/layout/helloworld_index_index.xml

<?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">
    <head>
        <title>Helloworld Landingspage</title>
    </head>
    <body>
        <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 -->
        </referenceContainer>
    </body>

app/code/Packt/HelloWorld/Block/Newproducts.php

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

        $this->_logger->info('YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY');
        //$this->_logger->info();

        $collection
            ->addAttributeToSelect('*')
            ->setOrder('created_at')
            ->setPageSize(5);

    return $collection;
}

}


app/code/Packt/HelloWorld/view/frontend/templates/newproducts.php

<h2>New Products</h2>

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

1 Answer 1

1

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.

1
  • @Muribury if my answer is helpful, you can accept it as the solution. Commented Jan 15, 2017 at 6:31

Your Answer

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.