Question

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.

Was it helpful?

Solution

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!

OTHER TIPS

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top