4

I'm trying to output custom phtml content of Lapisbard_General::customer_account.phtml in Magento_directory::currency.phtml

If I'm using below code, it works fine.

echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Lapisbard_General::customer_account.phtml")->toHtml();

But when I'm trying to use my custom block as below, it throws error

echo $this->getLayout()->createBlock("Lapisbard\General\Block\CustomerAccount")->setTemplate("Lapisbard_General::customer_account.phtml")->toHtml();

Fatal error: Call to a member function dispatch() on null in /var/www/html/lapis/vendor/magento/framework/View/Element/AbstractBlock.php on line 637

Block code:

namespace Lapisbard\General\Block;
use Magento\Customer\Model\Session;

class CustomerAccount extends \Magento\Framework\View\Element\Template {
    public function __construct(
        Session $customerSession
    )
    {
        $this->_customerSession = $customerSession;
    }
    public function isCustomerLoggedIn() {
        return $this->_customerSession->isLoggedIn();
    }
}

How can I make it work ?

  • add your block file code here – John Feb 26 '16 at 9:43
  • @John: Added block code. Kindly check. – amitshree Feb 26 '16 at 9:51
14

Replace below code with your block code

namespace Lapisbard\General\Block;
use Magento\Customer\Model\Session;

class CustomerAccount extends \Magento\Framework\View\Element\Template {
    public function __construct(\Magento\Framework\View\Element\Template\Context $context,
        Session $customerSession
    )
    {
        parent::__construct($context);
        $this->_customerSession = $customerSession;
    }
    public function isCustomerLoggedIn() {
        return $this->_customerSession->isLoggedIn();
    }
}

Then you can use it

echo $this->getLayout()->createBlock("Lapisbard\General\Block\CustomerAccount")->setTemplate("Lapisbard_General::customer_account.phtml")->toHtml();
0

If would be equal to 2x, use x^2 to calculate the underlying customerdata.

Store the data in an array and sort out the first 3 numbers.

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.