Magento 2自定义小部件设置模板数据不呈现在前端


Magento 2 Custom Widget set template data not render at frontend

我正在努力创建自定义扩展中的自定义小部件。我按照这个方法在我的自定义扩展中创建小部件。所有工作正常,但数据没有显示在模板$this->setTemplate('widget/viewed_list.phtml');在我的代码下面:

Technologymindz Instagramfeed/块/部件/Instawidget.php

<?php
namespace Technologymindz'Instagramfeed'Block'Widget;
class Instawidget extends 'Magento'Framework'View'Element'Template implements 'Magento'Widget'Block'BlockInterface
{
    public function _toHtml()
    {
        $this->setTemplate('widget/viewed_list.phtml');
    }

}

Technologymindz Instagramfeed/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="tm_customwidget" class="Technologymindz'Instagramfeed'Block'Widget'Instawidget">
        <label translate="true">Instagram Feeds</label>
        <description>Show Your Instagram Feeds Anywhere</description>
        <parameters>
                        <parameter name="tmfeedtoshow" xsi:type="select" required="true" visible="true">
                            <label>Show Latest Feeds</label>
                            <options>
                                <option name="default" value="12" selected="true">
                                    <label translate="true">12</label>
                                </option>
                                <option name="list" value="24">
                                    <label translate="true">24</label>
                                </option>                              
                            </options>
                        </parameter>
            <parameter name="tmview_type" xsi:type="select" required="true" visible="true">
                            <label>Select View Type</label>
                            <options>
                                <option name="default" value="widget/viewed_grid.phtml" selected="true">
                                    <label translate="true">Grid View</label>
                                </option>
                                <option name="list" value="widget/viewed_list.phtml">
                                    <label translate="true">List View</label>
                                </option>                              
                            </options>
                        </parameter>
        </parameters>                
    </widget>
</widgets>

Technologymindz/Instagramfeed/视图/前端/部件/viewed_list.phtml

<?php
echo $this->getTmview_type();
echo 'Welcome';
?>

数据输出不显示viewed_list.phtml,但如果我在块_toHtml()函数中设置了一些东西,它会显示。

public function _toHtml()
    {
        return '<p class="hello">Hello world!</p>';
    }

我想在.phtml管理输出而不是块,希望我能得到修复这个或任何更好的教程Magento 2自定义小部件数据呈现在模板。

好的,经过大量的研究,我在这里找到了一篇温和的文章,并为我解决了这个问题。我完全删除了_toHtml功能

解决方案为我工作:

protected function _construct()
    {
        parent::_construct();
        $this->setTemplate('widget/viewed_list.phtml');
    }