Load block outside Magento, and apply current template

Viewed 15619

I have a Magento installation which is integrated with an external website, and I want the Magento's shopping cart block to be displayed on the header of this external site.

I have achieved this with the following code:

<?php

require_once(dirname(__FILE__).'/store/app/Mage.php');

$app = Mage::app();
$session = Mage::getSingleton('core/session', array('name'=>'frontend'));

$block = $app
    ->getLayout()
    ->getBlockSingleton('checkout/cart_sidebar')
    ->setTemplate('checkout/cart/sidebar.phtml');

echo $block->toHtml();

But, I want (and believe that it's possible) a nicer approach.

I dislike the fact that I must specify the template manually via setTemplate(), which involves hard-coding template locations and repeating something that it's defined somewhere else (in the design's layout xml files). I tried loading the block via $app->getLayout()->getBlock($name) with no results (were $name represents the block's reference name, as defined in the layout xml files).

So the question is:

Is there any way to render a block outside magento (with the following requisites)?

  • I want the base layout xml and the store's design layout updates of the design changes to be loaded automatically, so i don't need to specify the template path and the block type (again) manually.
  • I want to load the block by it's reference name, so I can make use of the properties applied to it on the layout xml files.

The purpose of this question is to wrap it in a function, and render every block outside Magento the same way it's done on the Magento templates. For example:

<div id="sidebar-cart-container">
    <?php echo $this->renderMagentoBlock('cart-block-reference-id'); ?>
</div>

Thanks in advance.

1 Answers
Related