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
sabarivenkatesankrish
4294 silver badges13 bronze badges
2 Answers 2
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
Raul Sanchez
2,6643 gold badges30 silver badges67 bronze badges
-
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– sabarivenkatesankrish2018年06月08日 11:43:18 +00:00Commented 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 slashsabarivenkatesankrish– sabarivenkatesankrish2018年06月08日 11:44:57 +00:00Commented Jun 8, 2018 at 11:44 -
which error do you see? or it just doesn't print anything? any info in var/log files?Raul Sanchez– Raul Sanchez2018年06月08日 12:38:00 +00:00Commented 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'sabarivenkatesankrish– sabarivenkatesankrish2018年06月08日 12:57:38 +00:00Commented Jun 8, 2018 at 12:57
-
Try setting just html/header.phtml, without specifying moduleRaul Sanchez– Raul Sanchez2018年06月08日 13:13:29 +00:00Commented Jun 8, 2018 at 13:13
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
user68116
-
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 layoutsabarivenkatesankrish– sabarivenkatesankrish2018年06月08日 11:40:05 +00:00Commented Jun 8, 2018 at 11:40
default
Invalid template file: 'Magento_Theme::html/footer.phtml' in module: 'Magento_Theme' block's name: 'html\\footer_0'$this->_view->getLayout()->createBlock('Magento\Theme\Block\Html\Header')->toHtml();