在Magento外部加载块,并应用当前模板 [英] Load block outside Magento, and apply current template

查看:67
本文介绍了在Magento外部加载块,并应用当前模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Magento安装程序,该安装程序与外部网站集成在一起,我希望将Magento的购物车块显示在此外部站点的标题上.

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.

我不喜欢必须通过setTemplate()手动指定模板的事实,这涉及对模板位置进行硬编码,并重复在其他地方(在设计的布局xml文件中)定义的内容.我尝试通过$app->getLayout()->getBlock($name)加载该块而没有任何结果($name表示该块的引用名称,如布局xml文件中所定义).

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).

所以问题是:

有什么方法可以在magento之外渲染块(具有以下条件)?

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

  • 我希望基本布局xml和设计更改的商店的设计布局更新能够自动加载,因此我不需要手动(再次)指定模板路径和块类型.
  • 我想按其引用名称加载块,因此可以利用布局xml文件上应用于该块的属性.

此问题的目的是将其包装在函数中,并以与在Magento模板上相同的方式呈现Magento外部的每个块.例如:

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>

谢谢.

推荐答案

花了我几分钟的调试时间,但这似乎相对容易.

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;

请注意,您必须手动指定要从中加载块的布局句柄. 默认"布局句柄将包含侧边栏,因为它是从checkout.xml内部放置的.

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.

但是使用默认"布局句柄会带来巨大的性能成本,因为许多模块将其块放置在此句柄中.您可能需要将在外部站点上使用的所有块放在单独的布局句柄中,然后简单地加载它.

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.

这篇关于在Magento外部加载块,并应用当前模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆