15

I have an external webpage and I'm trying to pull the site header block from the Magento framework to use as a header in this external page. This is what I have so far:

// initialize Magento
$rootPath = dirname(dirname(__FILE__));
$mageInc = $rootPath . "/app/Mage.php";
include_once $mageInc;

Mage::app('admin')->setCurrentStore(0);

$headerBlock = Mage::app()->getLayout()->createBlock('page/html_header');
//also tried
//$headerBlock = Mage::app()->getLayout()->createBlock('page/html_header', 'header');

Execution stops there, however, and I get no error messages. My goal is to pull block Mage_Page_Block_Html_Header, which I think I can use to load <block type="page/html_header" name="header" as="header"> and all its contents from the page.xml layout file. That XML tag is wrapped inside

<default translate="label" module="page">
  <block type="page/html" name="root" output="toHtml" template="page/1column.phtml">

tags, so maybe I need to specify that somehow? I'm not sure what I'm doing wrong and where to go from here.

  • this code works but i have a problem with the logout button, some one have te same issue? Regards – Massimo Camoni Aug 26 '16 at 15:36
12

I found the secret ingredient.. and it is Mage::app()->loadArea('frontend');

<?php
include_once "app/Mage.php";
umask(0);
Mage::app()->loadArea('frontend');

$layout = Mage::getSingleton('core/layout');

//load default xml layout handle and generate blocks
$layout->getUpdate()->load('default');
$layout->generateXml()->generateBlocks();

//get the loaded head and header blocks and output
$headBlock = $layout->getBlock('head');
$headerBlock = $layout->getBlock('header');
echo $headBlock->toHtml() . $headerBlock->toHtml();

Thanks @benmarks!

3

You're 99% there. You need to call toHtml() on the block, and then echo it out to see the result:

<?php
// initialize Magento
$rootPath = dirname(dirname(__FILE__));
$mageInc = $rootPath . "/app/Mage.php";
include_once $mageInc;

Mage::app('admin')->setCurrentStore(0);

echo $headerBlock = Mage::app()->getLayout()->createBlock('page/html_header')->toHtml();
  • Yes, I have tried that and nothing. This is the answer though, I tried it on a new install and it works. Thanks. – Nick Rolando May 24 '13 at 3:24
  • I got this code working, however it doesn't give me near anything that the site header has. The site header has several logos, menus, and links. This gives me a one of the many images in the header. Perhaps I'm not loading it correctly? – Nick Rolando May 30 '13 at 22:35

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.