Magento 2 - 空自定义小部件渲染

I try to create a custom widget on Magento 2 EE to list Products with custom attribute 'end_life' Yes/No type. It's OK in backoffice, I created the widget with type "End Life Products" and I inserted it on my Homepage.

But the render on Homepage is empty. Just a <p></p>

Please help me to render my products list :)

app/code/My/CustomModule/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="My_CustomModule" setup_version="1.0.0">
    </module>
</config>

app/code/My/CustomModule/etc/widget.xml

<?xml version="1.0" encoding="UTF-8"?>
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Magento/Widget/etc/widget.xsd">
    <widget id="my_custommodule_end_life_products" class="My\CustomModule\Block\Widget\EndLifeProducts">
        <label translate="true">End Life Products</label>
        <description></description>
    </widget>
</widgets>

app/code/My/CustomModule/Block/Widget/EndLifeProducts.php

namespace My\CustomModule\Block\Widget;

class EndLifeProducts extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{

    public function getEndLifeCollection() {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
        $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
        $productCollection->addAttributeToFilter('end_life', true)
            ->load();
        return $productCollection;
    }

    public function _toHtml()
    {
        $this->setTemplate('widget/end_life_products.phtml');
    }
}

app/code/My/CustomModule/view/frontend/widget/end_life_products.phtml

<h1>End Life Products</h1>
<?php
if ($exist = ($block->getEndLifeCollection() && $block->getEndLifeCollection()->getSize())) {
    $items = $block->getEndLifeCollection()->getItems();
}

I found a solution :

Define your template as protected $_template variable, and don't define _toHtml() function

namespace My\CustomModule\Block\Widget;

class EndLifeProducts extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{

    protected $_template = 'widget/end_life_products.phtml';

    public function getEndLifeCollection() {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
        $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
        $productCollection->addAttributeToFilter('end_life', true)
            ->load();
        return $productCollection;
    }
}

And put your template .phtml in your custom module template folder : app/code/My/CustomModule/view/frontend/template/widget/end_life_products.phtml

move your file end_life_products.phtml to app/code/My/CustomModule/view/frontend/templates/widget/end_life_products.phtml