5

I am working to create custom widget in custom extension. I followed this to create widget in my custom extension.
All working fine but data not showing in the template
$this->setTemplate('widget/viewed_list.phtml');

Below my code:

Technologymindz/Instagramfeed/Block/Widget/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/view/frontend/widget/viewed_list.phtml

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

Data output not showing for viewed_list.phtml but if i set something in Block _toHtml()function than it shows.

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

Hope this is clear, I want to manage output in .phtml not in Block

2

For me it was Peter's solution, but also move your template file to /view/templates/widget/viewed_list.phtml

public function _toHtml()
{
    $this->setTemplate('widget/viewed_list.phtml');
    return parent::_toHtml();
}

And /view/templates/widget/viewed_list.phtml instead of /view/widget/viewed_list.phtml

1

i did removed _toHtml function

Here the solution fix for me:

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

    }
0

In your modification of the _toHtml you are both preventing the logic from the parent class from triggering and stopping it returning anything. The reason your example at the bottom works is that your method actually returns content. There are a few different variations, but something along the lines of this should work:

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

    return parent::_toHtml();
}
2
0

Having had the same problem with this exact tutorial, and several cloned versions of it that appear high up the search listings, I came to the conclusion that it's a pretty buggy and poor quality example that's been rush-written for SEO.

I eventually found this widget tutorial that seems far more accurate: https://magently.com/blog/magento-2-widgets-tutorial/ - I'm sure there are others. Lots of the differences make me think the venustheme one follows bad practise.

An example in your/venustheme's code is ../../../Magento/Widget/etc/widget.xsd instead of urn:magento:module:Magento_Widget:etc/widget.xsd. Both work, but the latter seems to follow the practise of the core modules.

0
class BannerWidgetBlock extends Template implements BlockInterface
{
    const DEFAULT_SELECTION = 1;

    protected $_template = "widget/banner-widget.phtml";
}

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.