0

我怎样才能得到输出:

<block type="page/template_links" name="top.links" as="topLinks"/>

在控制器动作中?

事实上,我必须通过 AJAX 显示链接。

4

4 回答 4

1

如果我对您的理解正确,您可以在您的操作功能中执行此操作:

$linksBlock = $this->getLayout()->createBlock('page/template_links');
echo $linksBlock->toHtml();

要通过 ajax 调用获取内容,请添加以下 js 脚本:

new Ajax.Updater('your_container', '/your_controller/your_action');
于 2012-06-12T10:15:34.287 回答
1
$this->getLayout()->getBlock('top.links')->toHtml()

请试试这个,我已经检查过了,它的工作原理。

于 2012-06-12T11:01:06.090 回答
1

虽然提供的赞成答案确实回答了这个问题,但它们都忽略了常见情况,即使用 AJAX 加载的块所采取的某些特定操作将阻止 Magento 重定向按预期工作。

采取以下情况:

  • 通过 AJAX 加载的块
  • 块有“添加到购物车”按钮(可能由getAddUrl($product)助手生成)
  • 系统配置>>结帐>>购物车>>'将产品重定向到购物车后' == NO

在这种情况下,单击“添加到购物车”后,Magento 将尝试使用 CartController.php 的_goBack()方法重定向用户。这会导致您之前通过 AJAX 加载的块在页面上呈现,而不是用户实际被重定向到他们所在的页面。


为防止这种情况发生,'your_param'请向加载您的块的 AJAX 请求添加一个附加参数,并为其指定一个值Mage::helper('core/url')->getCurrentUrl()(或您可能想要的任何其他值)。如:

jQuery.ajax({
    url: '<?php echo $yourUrl ?>',
    page: '<?php echo Mage::helper("core/url")->getCurrentUrl()?>'
})

Then, in your controller's relevant action method, add a quick condition to the beginning of the method, such as:

if (!this->getRequest()->isXmlHttpRequest()) {
    $this->getResponse()->setRedirect($this->getRequest()->getParam('your_param'));
}
// ... existing controller code ...
// ... ie: $block->toHtml() ...

This will allow the user to return to their previous page after adding to cart. This also will provide a way for the user to return to the site if they happen to end up at your controller's URL.

于 2014-01-08T02:45:58.073 回答
-1

您可以查看http://www.magentocommerce.com/magento-connect/catalog/product/view/id/18152/s/ajax-blocks-6911/ 此模块允许您使用 ajax 加载块,只需通过你的布局文件

于 2013-06-01T12:55:29.707 回答