我在 catalog/navigation/vert_nav.phtml 中尝试了以下两种方法来添加或抑制特定于主页的内容:
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 缓存打开时,它首先工作,然后在一段时间后主页开始显示仅供其他页面使用的内容(在我使用 lower down 的 else 子句之后)。当我关闭 BLOCK_HTML 时,它的行为符合预期。
有趣的是,我在 page/html/head.phtml(针对主页特定的 javascript/css)和 page/html/header.phtml(对于标题横幅应该仅出现在主页上),即使 BLOCK_HTML 处于开启状态,这些也能正常工作。
(Magento 1.4.1.1)
最佳答案
上面的答案是最好的解决方案。
你可以简单地复制 app/code/core/Mage/Catalog/Block/Nagivation.php
到:
app/code/local/Mage/Catalog/Block/Nagivation.php
然后如上所述更改 getCacheKeyInfo() 方法。
/**
* 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;
}
这将使主页/非主页页面的缓存键不同,这将缓存两个副本,而不是缓存单个模板副本以供所有页面使用。
关于php - 在启用 BLOCK_HTML 缓存的情况下检测 Magento .phtml 中的主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12403675/