Load / Get a block outside of Magento routing for external use

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!


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();