I have created a simple widget which displays dummy data. The problem is my front end code doesn't show up in my page ? I 'm working on Magento 2.2.0.

Here is my widget code what i want to display :

{{widget type="Widget\CustomWidget\Block\Widget\ContactInformations" fullname="Adrien" gender="mal" age="24"}}

widget/contact_informations code :

$fullname = $this->getData('fullname');
$age = $this->getData('age');
$gender = $this->getData('gender');
    <?php if($fullname !=''){?>
    <li><?php echo $fullname; ?></li>
    <?php } ?>
    <?php if($age !=''){?>
    <li><?php echo $age; ?></li>
    <?php } ?>
    <?php if($gender!=''){?>

    <?php } ?>

etc/module.xml code :

<?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="Widget_CustomWidget" setup_version="1.0.0">

etc/widget.xml code :

<?xml version="1.0" encoding="UTF-8"?>
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
    <widget id="widget_customwidget" class="Widget\CustomWidget\Block\Widget\ContactInformations">
        <label translate="true">Contact Informations Widget</label>
        <description>Widget in Magento2</description>
            <parameter name="fullname" xsi:type="text"  visible="true" sort_order="0" >
                <label translate="true">Full Name</label>
            <parameter name="age" xsi:type="text"  visible="true" sort_order="10" >
                <label translate="true">Age</label>
            <parameter name="gender" xsi:type="select" source_model="Widget\CustomWidget\Model\Config\Source\Gender" visible="true" sort_order="10" >
                <label translate="true">Gender</label>

Widget/CustomWidget/Block/Widget/ContactInformations code :

namespace Widget\CustomWidget\Block\Widget; 
class ContactInformations extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
    public function _toHtml()

Any idea ?

  • You should post your custom widget code here? – Khoa TruongDinh Dec 1 '16 at 9:08
  • I updated my question. – Adrien Castagliola Dec 1 '16 at 9:19
  • can you show the code for Widget\CustomWidget\Block\Widget\ContactInformations ? is it extending \Magento\Framework\View\Element\Template or other widget block? – Miroslav Petroff Dec 1 '16 at 9:29
  • Question updated @MiroslavPetroff – Adrien Castagliola Dec 1 '16 at 9:38
  • @AdrienCastagliola instead of using _toHtml() , Use _construct function it will work - try this code - protected function _construct() { parent::_construct(); $this->setTemplate('widget/contact_informations.phtml'); } – Manthan Dave Dec 1 '16 at 9:39

Your widget block Widget\CustomWidget\Block\Widget\ContactInformations should extend \Magento\Framework\View\Element\Template otherwise it can't be displayed.

Everything is ok in your configuration code. However your Block code is wrong. You shouldn't override _tomHtml() in this case but define your template as protected $_template = 'widget/contact_informations.phtml'. Your template must be stored to this path, at the root of your module: /view/frontend/widget/contact_informations.phtml

namespace Widget\CustomWidget\Block\Widget;

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

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

  • It already does. – Adrien Castagliola Dec 1 '16 at 9:44
  • 1
    <?php namespace Widget\CustomWidget\Block\Widget; class ContactInformations extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface { $_template = 'widget/contact_informations.phtml'; } – Mr.K Dec 1 '16 at 9:51
  • Try to replace your code by the code above – Mr.K Dec 1 '16 at 9:52
  • My code expects a function i think. – Adrien Castagliola Dec 1 '16 at 9:56
  • Actually your method '_toHtml' is completely wrong.. Please do not override _toHtml() and override the member class $_template with your template value. – Mr.K Dec 1 '16 at 10:00

Your widget class should be like this:

namespace Widget\CustomWidget\Block\Widget; 
class ContactInformations extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        array $data = []
    ) {
        parent::__construct($context, $data);

    public function _toHtml()
        return parent::_toHtml();

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.