久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Magento 之外加載塊,并應用當前模板

Load block outside Magento, and apply current template(在 Magento 之外加載塊,并應用當前模板)
本文介紹了在 Magento 之外加載塊,并應用當前模板的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個與外部網站集成的 Magento 安裝,我希望 Magento 的購物車塊顯示在這個外部網站的標題上.

我使用以下代碼實現了這一點:

<?phprequire_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');回聲 $block->toHtml();

但是,我想要(并且相信這是可能的)更好的方法.

我不喜歡我必須通過 setTemplate() 手動指定模板的事實,這涉及硬編碼模板位置并重復它在其他地方(在設計的布局 xml 文件中)定義的內容.我嘗試通過 $app->getLayout()->getBlock($name) 加載塊,但沒有結果($name 表示塊的引用名稱,如在布局 xml 文件中定義).

所以問題是:

有沒有辦法在 magento 之外渲染一個塊(具有以下必要條件)?

  • 我希望自動加載基本布局 xml 和商店的設計更改的設計布局更新,因此我不需要(再次)手動指定模板路徑和塊類型.
  • 我想通過引用名稱加載塊,這樣我就可以在布局 xml 文件中使用應用于它的屬性.

這個問題的目的是將它包裝在一個函數中,并以與在 Magento 模板上所做的相同的方式在 Magento 之外渲染每個塊.例如:

提前致謝.

解決方案

花了我幾分鐘的調試時間,但似乎相對容易.

getLayout();$layout->getUpdate()->addHandle('默認')->addHandle('some_other_handle')-> 加載();/** 生成塊,但必須是來自先前加載的布局句柄的 XML* 首先加載.*/$layout->generateXml()->generateBlocks();/** 現在我們可以簡單地以通常的方式獲取任何塊.*/$cart = $layout->getBlock('cart_sidebar')->toHtml();回聲 $cart;

請注意,您必須手動指定要從中加載塊的布局句柄.默認"布局句柄將包含側邊欄,因為它位于 checkout.xml 中.

但是使用默認"布局句柄會帶來顯著的性能成本,因為許多模塊將它們的塊放置在此句柄中.您可能希望將您在外部站點上使用的所有塊放在一個單獨的布局句柄中,然后簡單地加載它.

選擇權在你.祝你好運.

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.

解決方案

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.

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 之外加載塊,并應用當前模板的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Magento products by categories(按類別劃分的 Magento 產品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶帳戶注冊事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯誤 404在 Magento 管理員登錄頁面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: 国产精品麻 | 国产99久久精品一区二区永久免费 | 日韩欧美国产一区二区 | 美女久久视频 | 久久午夜精品福利一区二区 | 免费簧片视频 | 日本爱爱视频 | 亚洲综合精品 | 久久精品一区 | 超碰av人人| 国产精品一区二区三区四区 | 综合二区 | 亚洲最大的成人网 | 日韩欧美在线播放 | www.日韩 | 8x国产精品视频一区二区 | 国产午夜精品一区二区三区四区 | 手机看黄av免费网址 | 希岛爱理在线 | 精品九九 | 亚洲欧美一区二区三区在线 | 国产精品久久久久久久久污网站 | 亚洲一区二区三区桃乃木香奈 | 中文字字幕一区二区三区四区五区 | 欧美黄色精品 | 久久久久久免费看 | 欧美一区二区三区在线观看 | 日韩欧美三级在线 | 日日夜夜视频 | 亚洲国产一区二区三区在线观看 | 亚洲一区二区视频在线观看 | 久草视频在线播放 | 国产精品久久毛片av大全日韩 | 蜜桃视频一区二区三区 | 永久免费av | 国产一区在线免费 | 成人午夜免费视频 | 欧美精品久久久久 | 日韩精品在线观看免费 | 欧美激情久久久 | 成人av影院 |