在Magento外面调用块布局是不同的

时间:2011-07-20 12:44:50

标签: layout magento external block xml-layout

我使用了这里建议的片段

Load block outside Magento, and apply current template

在Magento外面显示一个区块 这是我的代码:

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

$layout = Mage::app()->getLayout();
$layout->getUpdate()
    ->addHandle('default')
    ->load();

$layout->generateXml()
       ->generateBlocks();

$header = $layout->getBlock('header')->toHtml();
echo $header;

打印标题,但topLinks与Magento中显示的不同。
更重要的是,html略有不同(缺少一些div) 这是我的XML布局:

<block type="page/html_header" name="header" as="header">
    <block type="core/text_list" name="top.menu" as="topMenu" translate="label">
        <label>Navigation Bar</label>
    </block>
    <block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
    <block type="page/template_links" name="top.links" as="topLinks"/>
    <block type="page/html_wrapper" name="top.bar" as="topBar" translate="label">
        <label>Breadcrumbs</label>
        <action method="setElementClass"><value>top-bar</value></action>
        <block type="page/html_breadcrumbs" name="breadcrumbs" as="breadcrumbs"/>
    </block>
    <block type="page/html_wrapper" name="top.container" as="topContainer" translate="label">
        <label>Page Header</label>
        <action method="setElementClass"><value>top-container</value></action>
    </block>
</block>

怎么了?

更新

感谢Alan Storm的建议,现在我知道错误并没有设置正确的句柄 我需要将customer_logged_in或customer_logged_out添加到$ layout 我试过

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

$login_status = '';
if($session->isLoggedIn()){
    $login_status = 'customer_logged_in';
} else {
    $login_status = 'customer_logged_out';
} 

但是我的用户结果即使在登录时也总是退出 我错过了什么?

1 个答案:

答案 0 :(得分:0)

对于初学者,您只加载了一个布局句柄。除default句柄外,每个Magento页面请求都会加载商店的句柄(例如STORE_english )以及指示客户是否已记录inout的句柄(如{1}}作为customer_logged_out)。如果没有这些句柄,某些事情就不会发生,最终呈现的页面看起来会有所不同。