0

I have tried this solution but it is not working

$this->_layout->createBlock('Vendor\Module\Block\Productdesigner');
       $response = $layout->setData(array("customer_data" => $result))->setTemplate('Vendor_Module::test.phtml')->toHtml();

get an error when i have used above solution

Invalid template file: 'Vendor_Module::test.phtml' in module: 'Vendor_Module' block's name: 'productdesigner_0

I have add phtml file in below path

app/code/Vendor/Module/view/frontend/templates/test.phtml
3
  • Please clarify more to make it understandable. Feb 4, 2020 at 5:29
  • what you are trying to do? Feb 4, 2020 at 5:30
  • I need to get phtml file data using helper file. Feb 4, 2020 at 5:50

3 Answers 3

2

Please use this

<?php echo $block->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate('Vendor_Module::test.phtml')->toHtml(); ?>
1

For Ex. if you call helper function from controller, then you can call PHTML and get data using following way :

/app/code/Vendor/Module/Controller/Index/Index.php

<?php

namespace Vendor\Module\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Framework\App\Request\Http
     */
    protected $request;

    /**
     * @var \Vendor\Module\Helper\Data
     */
    protected $helperData;

    /**
     * [__construct description]
     * @param \Magento\Framework\App\Request\Http   $request    [description]
     * @param \Magento\Framework\App\Action\Context $context    [description]
     * @param \Vendor\Module\Helper\Data            $helperData [description]
     */
    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\App\Action\Context $context,
        \Vendor\Module\Helper\Data $helperData
    ) {
        $this->request = $request;
        $this->helperData = $helperData;
        parent::__construct($context);
    }

    public function execute()
    {
        return $this->helperData->getPhtml();
    }
}

/app/code/Vendor/Module/Helper/Data.php

<?php

namespace Vendor\Module\Helper;

use Magento\Framework\View\Result\PageFactory;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Framework\Registry
     */
    protected $registry;

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * [__construct description]
     * @param \Magento\Framework\Registry $registry          [description]
     * @param PageFactory                 $resultPageFactory [description]
     */
    public function __construct(
        \Magento\Framework\Registry $registry,
        PageFactory $resultPageFactory

    ) {
        $this->registry = $registry;
        $this->resultPageFactory = $resultPageFactory;
    }

    public function getPhtml()
    {
        $resultPage = $this->resultPageFactory->create();
        $block = $resultPage->getLayout()
            ->createBlock('Magento\Framework\View\Element\Template')
            ->setTemplate('Vendor_Module::test.phtml')
            ->toHtml();
        print_r($block);
    }
}

/app/code/Vendor/Module/view/frontend/templates/test.phtml

<?= "PHTML file"; ?>

I hope it will helpful for you.

0

You can try,

<?php echo $block->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate('Vendor_Module::test.phtml')->toHtml(); ?>
1
  • Thank you @Kazim But it is get error "Notice: Undefined variable: block in". I need to get phtml file data using helper file. Feb 4, 2020 at 5:49

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.