2

I want to get my header part as html and I will use this in some other site with API call. For that I am trying to render using Magento\Theme\Block\Html\Header block and the following code:

$this->_view->getLayout()
    ->createBlock('Magento\Theme\Block\Html\Header')
    ->setTemplate('Magento_Theme::html/header.phtml')
    ->toHtml();

But it is not working. Can someone please suggest how to correct this or some other method to implement.

  • what you getting by this code? – kunj Jun 8 '18 at 11:20
  • I am getting the error like Invalid template file: 'Magento_Theme::html/footer.phtml' in module: 'Magento_Theme' block's name: 'html\\footer_0' – sabarivenkatesankrish Jun 8 '18 at 11:38
  • try without template $this->_view->getLayout()->createBlock('Magento\Theme\Block\Html\Header')->toHtml(); – kunj Jun 8 '18 at 12:15
  • I am trying to do this also, but I've got an empty string as result. Did you solved it? – vaso123 Mar 21 '19 at 10:59
0

Can't you use $block for any reason instead of $this->_view?

update

One more thing, template path should use / instead of \

Magento_Theme::html/header.phtml
  • particular reason for using the $this->_view, as mentioned in the post I am going to use this as API. So I need to get the html in model. In model it is not possible to use $block->getLayout() or $this->getLayout(). – sabarivenkatesankrish Jun 8 '18 at 11:43
  • Actually I was used the template as Magento_Theme::html/header.phtml. But in the post only by mistake I put the back slash – sabarivenkatesankrish Jun 8 '18 at 11:44
  • which error do you see? or it just doesn't print anything? any info in var/log files? – Raul Sanchez Jun 8 '18 at 12:38
  • Yes, I am getting the error like Invalid template file: 'Magento_Theme::html/footer.phtml' in module: 'Magento_Theme' block's name: 'html\\footer_0' – sabarivenkatesankrish Jun 8 '18 at 12:57
  • Try setting just html/header.phtml, without specifying module – Raul Sanchez Jun 8 '18 at 13:13
0

Try this code :

echo $this->getLayout()
    ->createBlock("Magento\Theme\Block\Html\Footer")
    ->setTemplate("Magento_Theme::html/footer.phtml")
    ->toHtml();
  • Hi, as I mentioned in the post, this is to be implemented as Api. I can't use $this class in model or even in controller to get the layout – sabarivenkatesankrish Jun 8 '18 at 11:40

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.