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.

Barry
5871 gold badge5 silver badges22 bronze badges
asked Jun 8, 2018 at 11:13
4
  • what you getting by this code? Commented Jun 8, 2018 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' Commented Jun 8, 2018 at 11:38
  • try without template $this->_view->getLayout()->createBlock('Magento\Theme\Block\Html\Header')->toHtml(); Commented Jun 8, 2018 at 12:15
  • I am trying to do this also, but I've got an empty string as result. Did you solved it? Commented Mar 21, 2019 at 10:59

2 Answers 2

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
answered Jun 8, 2018 at 11:23
5
  • 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(). Commented Jun 8, 2018 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 Commented Jun 8, 2018 at 11:44
  • which error do you see? or it just doesn't print anything? any info in var/log files? Commented Jun 8, 2018 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' Commented Jun 8, 2018 at 12:57
  • Try setting just html/header.phtml, without specifying module Commented Jun 8, 2018 at 13:13
0

Try this code :

echo $this->getLayout()
 ->createBlock("Magento\Theme\Block\Html\Footer")
 ->setTemplate("Magento_Theme::html/footer.phtml")
 ->toHtml();
Barry
5871 gold badge5 silver badges22 bronze badges
answered Jun 8, 2018 at 11:21
1
  • 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 Commented Jun 8, 2018 at 11:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.