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.
我有一个与外部网站集成的Magento安装,我希望Magento的购物车块显示在这个外部网站的标题上。
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).
我不喜欢我必须通过setTemplate()手动指定模板这一事实,它涉及硬编码模板位置并重复在其他地方定义的内容(在设计的布局xml文件中)。我尝试通过$ app-> getLayout() - > getBlock($ name)加载块而没有结果($ name表示块的引用名称,如布局xml文件中所定义)。
So the question is:
所以问题是:
Is there any way to render a block outside magento (with the following requisites)?
有没有办法在magento之外渲染一个块(具有以下必要条件)?
- 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.
我希望基本布局xml和设计更改的商店设计布局更新自动加载,所以我不需要手动指定模板路径和块类型(再次)。
我想通过它的引用名称加载块,所以我可以在布局xml文件上使用应用于它的属性。
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:
这个问题的目的是将它包装在一个函数中,并在Magento外部渲染每个块,就像在Magento模板上完成一样。例如:
<div id="sidebar-cart-container">
<?php echo $this->renderMagentoBlock('cart-block-reference-id'); ?>
</div>
Thanks in advance.
提前致谢。
1 个解决方案
#1
23
Took me a couple minutes of debugging, but it seems relatively easy.
我花了几分钟的调试,但似乎相对容易。
<?php
/*
* Initialize magento.
*/
require_once 'app/Mage.php';
Mage::init();
/*
* Add specific layout handles to our layout and then load them.
*/
$layout = Mage::app()->getLayout();
$layout->getUpdate()
->addHandle('default')
->addHandle('some_other_handle')
->load();
/*
* Generate blocks, but XML from previously loaded layout handles must be
* loaded first.
*/
$layout->generateXml()
->generateBlocks();
/*
* Now we can simply get any block in the usual way.
*/
$cart = $layout->getBlock('cart_sidebar')->toHtml();
echo $cart;
Please note that you must manually specify which layout handles you want to load blocks from. The 'default' layout handle will contain the sidebar since it is placed there from inside checkout.xml.
请注意,您必须手动指定要从中加载块的布局句柄。 “默认”布局句柄将包含侧边栏,因为它位于checkout.xml内部。
But using the 'default' layout handle can come with a significant performance cost since many modules place their blocks in this handle. You may want to put all the blocks that you use on your external site in a separate layout handle and simply load that.
但是使用“默认”布局句柄会带来显着的性能成本,因为许多模块将其块放在此句柄中。您可能希望将您在外部站点上使用的所有块放在单独的布局句柄中,然后只需加载它。
The choice is yours. Good luck.
这是你的选择。祝好运。
#1
23
Took me a couple minutes of debugging, but it seems relatively easy.
我花了几分钟的调试,但似乎相对容易。
<?php
/*
* Initialize magento.
*/
require_once 'app/Mage.php';
Mage::init();
/*
* Add specific layout handles to our layout and then load them.
*/
$layout = Mage::app()->getLayout();
$layout->getUpdate()
->addHandle('default')
->addHandle('some_other_handle')
->load();
/*
* Generate blocks, but XML from previously loaded layout handles must be
* loaded first.
*/
$layout->generateXml()
->generateBlocks();
/*
* Now we can simply get any block in the usual way.
*/
$cart = $layout->getBlock('cart_sidebar')->toHtml();
echo $cart;
Please note that you must manually specify which layout handles you want to load blocks from. The 'default' layout handle will contain the sidebar since it is placed there from inside checkout.xml.
请注意,您必须手动指定要从中加载块的布局句柄。 “默认”布局句柄将包含侧边栏,因为它位于checkout.xml内部。
But using the 'default' layout handle can come with a significant performance cost since many modules place their blocks in this handle. You may want to put all the blocks that you use on your external site in a separate layout handle and simply load that.
但是使用“默认”布局句柄会带来显着的性能成本,因为许多模块将其块放在此句柄中。您可能希望将您在外部站点上使用的所有块放在单独的布局句柄中,然后只需加载它。
The choice is yours. Good luck.
这是你的选择。祝好运。