1

我使用了这里建议的片段

在 Magento 外部加载块,并应用当前模板

在 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';
} 

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

4

1 回答 1

0

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

于 2011-07-20T17:18:51.920 回答