在Magento .phtml中检测可启用BLOCK_HTML缓存的主页 [英] Detect home page in Magento .phtml that will work with BLOCK_HTML cache enabled
问题描述
我已经尝试在catalog/navigation/vert_nav.phtml中使用以下两种方法来添加或隐藏特定于主页的内容:
I have tried the following two methods in catalog/navigation/vert_nav.phtml to add or suppress content specific to the home page:
if($this->getUrl('') == $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true))):
或
if(
Mage::getSingleton('cms/page')->getIdentifier() == 'home' &&
Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms'
) :
两者都能正常工作,但是,当打开BLOCK_HTML缓存时,它首先会工作,然后过一会儿,主页开始显示仅用于其他页面的内容(在else子句之后,我使用下面的文字).当我关闭BLOCK_HTML时,它的行为符合预期.
Both work fine, however when BLOCK_HTML cache is turned on, it works at first, then after a while the home page starts displaying content that is intended only for other pages (after an else clause I use lower down). When I turn off the BLOCK_HTML, it behaves as expected.
有趣的是,我在page/html/head.phtml(用于特定于首页的javascript/css)和page/html/header.phtml(用于应显示的标题横幅)中使用了相同的代码(第1个)仅显示在首页上),即使BLOCK_HTML处于启用状态,它们也可以正常工作.
Interestingly I've used the same code (the 1st one) in page/html/head.phtml (for home page specific javascript/css), and in page/html/header.phtml (for a header banner that should only appear on the home page), and these work fine even when BLOCK_HTML is ON.
(Magento 1.4.1.1)
(Magento 1.4.1.1)
推荐答案
以上答案是最佳解决方案.
The above answer is the best solution.
您只需复制app/code/core/Mage/Catalog/Block/Nagivation.php
You could simply copy app/code/core/Mage/Catalog/Block/Nagivation.php
收件人:
app/code/local/Mage/Catalog/Block/Nagivation.php
app/code/local/Mage/Catalog/Block/Nagivation.php
,然后如上所述更改getCacheKeyInfo()方法.
and then change the getCacheKeyInfo() method as described above.
/**
* Get Key pieces for caching block content
*
* @return array
*/
public function getCacheKeyInfo()
{
$shortCacheId = array(
'CATALOG_NAVIGATION',
Mage::app()->getStore()->getId(),
Mage::getDesign()->getPackageName(),
Mage::getDesign()->getTheme('template'),
Mage::getSingleton('customer/session')->getCustomerGroupId(),
'template' => $this->getTemplate(),
'name' => $this->getNameInLayout(),
$this->getCurrenCategoryKey(),
// Your logic to make home/none home have different cache keys
Mage::getSingleton('cms/page')->getIdentifier() == 'home' ? '1' : '0'
);
$cacheId = $shortCacheId;
$shortCacheId = array_values($shortCacheId);
$shortCacheId = implode('|', $shortCacheId);
$shortCacheId = md5($shortCacheId);
$cacheId['category_path'] = $this->getCurrenCategoryKey();
$cacheId['short_cache_id'] = $shortCacheId;
return $cacheId;
}
这将使主页/非主页页面的缓存键有所不同,后者将缓存两个副本,而不是缓存单个模板副本以供所有页面使用.
This will make the cache key different for homepage / none-homepage pages, which will cache two copies, rather than caching a single template copy for use on all pages.
这篇关于在Magento .phtml中检测可启用BLOCK_HTML缓存的主页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!