View our collection in our block template magento2

Today we will see how to get a collection and to display it in a magento2 template.

Reminder on the Magento2 MVC

Remember what we saw in the previous tutorials. When we go on http://www.votredomaine.lan/contacts/test/index Magento2 will call our action app/code/Pfay/Contacts/Controller/Test/Index.php :

namespace Pfay\Contacts\Controller\Test;

use Magento\Framework\App\Action\Action;

class Index extends Action
    public function execute()

In this action it loads the layout and display it. Magento will fetch the file app/code/Pfay/Contacts/view/frontend/layout/contacts_test_index.xml.

<?xml version="1.0"?>
<page xmlns:xsi="" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
            <block class="Pfay\Contacts\Block\Contactslist" name="contactForm" template="Pfay_Contacts::test_index.phtml"></block>

It knows the information to display for our action thanks to the directives contained in this layout file. Here magento display our block using the class Pfay\Contacts\Block\Contactslist for the "logical" part and the Pfay_Contacts::test_index.phtml template ie app/code/Pfay/Contacts/view/frontend/templates/test_index.phtml for the "view" (template) part.

Edit our magento2 block to get the collection

We will now modify our block so that it automatically takes a contact type object when it is created that will allow us to retrieve its collection and pass it to our template. Edit your Contactslist.php file like this:

namespace Pfay\Contacts\Block;
use Magento\Framework\View\Element\Template;

class Contactslist extends \Magento\Framework\View\Element\Template
    private $_contact;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Pfay\Contacts\Model\Contact $contact,
        \Magento\Framework\App\ResourceConnection $resource,
        array $data = []
    ) {
        $this->_contact = $contact;
        $this->_resource = $resource;


    public function getContacts()
        $collection = $this->_contact->getCollection();
        return $collection;

Show the collection items in the template

We will then edit our template file app/code/Pfay/Contacts/view/frontend/templates/test_index.phtml as follows:

$contacts = $this->getContacts();
<div id="contactslist">
        <?php foreach ($contacts as $contact): ?>
                <td><?php echo $contact->getName(); ?></td>
                <td><?php echo $contact->getEmail(); ?></td>
        <?php endforeach; ?>
            <td>there is no contact in this list</td>

You notice that we use the function getContacts() created in the block just above. Display your page (do not forget to empty your cache) and you should normally see your contact table displayed on the magento front office. Congratulations this is the end of this tutorial which is finally a reminder for the developers magento2 :)
Available documents for this article :
Books that can help you :
  • Livre Magento 2 Developer's Guide by Branko
  • Livre Mastering Magento2 Second Edition
  • Livre Magento 2 Cookbook
Questions about this lesson
No questions for this lesson. Be the first !

You must be logged in to ask for help on a lesson.