Detect home page in Magento .phtml that will work with BLOCK_HTML cache enabled
Asked Answered
L

5

6

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))):

or

if(
Mage::getSingleton('cms/page')->getIdentifier() == 'home'  &&
Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms' 
) :

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.

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)

Leaves answered 13/9, 2012 at 9:53 Comment(0)
F
4

The above answer is the best solution.

You could simply copy app/code/core/Mage/Catalog/Block/Nagivation.php

to:

app/code/local/Mage/Catalog/Block/Nagivation.php

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.

Flanigan answered 13/9, 2012 at 11:13 Comment(2)
great addition to my post, there's just a little typo in this line: Mage::getSingleton('cms/page')->getIdentifier() == 'home' ? '1', '0' - should be Mage::getSingleton('cms/page')->getIdentifier() == 'home' ? '1': '0' to work correctlyKenric
thanks for that, it was typed directly into the browser with no testing :)Flanigan
K
4

Here are sources you'd want to read about Block Html cache:

  1. magento forum
  2. some blog
  3. inchoo blog

It would be better for performance to not disable the block completely, but rather specify the cache key in a smart way. So here's what you should do:

  1. First - specify a custom block for your .phtml file. If you don't know what Block is, or how to assign a block to a template file, here's the reference to Alan Storm blog.
  2. Second - you will have to add next code to a Block constructor:

    $this->addData(array(
        'cache_lifetime' => 3600,
        'cache_tags'     => array(Mage_Cms_Model_Block::CACHE_TAG),
        'cache_key'      => $this->getCacheKey(),
    ));
    

    As you see, I used here the getCacheKey method from the abstract class Mage_Core_Block_Abstract.

  3. Now you need to make sure the cache_key works for your logic. The Mage_Core_Block_Abstract::getCacheKey uses other method, which should actually specify the unique values for our block - getCacheKeyInfo. You need to redefine it using your logic:

    public function getCacheKeyInfo()
    {
        $isHomepage = 0;
        if (Mage::getSingleton('cms/page')->getIdentifier() == 'home') {
            $isHomepage = 1;
        }
        return array(
            $this->getNameInLayout(),
            $isHomepage,
        );
    }
    

    Now you can be sure that cache key for Home Page will differ from cache key to all other your pages, and your cache will return valid info.

Kenric answered 13/9, 2012 at 10:32 Comment(1)
Excellent answer, thanks. I ended up selecting Andrew's answer instead since this was a quick fix that worked with a template I hadn't originally authored.Leaves
F
4

The above answer is the best solution.

You could simply copy app/code/core/Mage/Catalog/Block/Nagivation.php

to:

app/code/local/Mage/Catalog/Block/Nagivation.php

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.

Flanigan answered 13/9, 2012 at 11:13 Comment(2)
great addition to my post, there's just a little typo in this line: Mage::getSingleton('cms/page')->getIdentifier() == 'home' ? '1', '0' - should be Mage::getSingleton('cms/page')->getIdentifier() == 'home' ? '1': '0' to work correctlyKenric
thanks for that, it was typed directly into the browser with no testing :)Flanigan
L
2

We use

<!-- SNH CUSTOM -->

    $route = Mage::app()->getFrontController()->getRequest()->getRouteName();

    $action = Mage::app()->getFrontController()->getRequest()->getActionName();

if($route == 'cms' && $action == 'index'):

    <div class="grid_12">

        echo $this->getChildHtml('shopper_footer_partners');

    </div>

endif;
Lacreshalacrimal answered 12/8, 2014 at 12:1 Comment(0)
R
2

Just to add to these answers suggesting to check if current page identifier equals to "home".

It would be definitely safer to compare it with Mage::getStoreConfig('web/default/cms_home_page') instead.

Remillard answered 26/5, 2015 at 17:19 Comment(0)
L
0

really the best way is to:

1 Update your layout XML (local.xml or theme custom.xml)

<!--  CUSTOM: ADD NEW FOOTER BLOCK AT BOTTOM FOR PARTNERS -->
<cms_index_index>
    <reference name="footer">
    <block type="cms/block" name="footer_block_extra">
        <action method="setBlockId"><block_id>footer_block_extra</block_id></action>
    </block>
    </reference>
</cms_index_index>

and step 2 add this code where you want the block in your template phtml (often /page/html/footer.phtml)

<!-- SNH CUSTOM -->
<div class="grid_12">
    <?php echo $this->getBlockHtml('footer_block_extra'); ?>
</div>

and step 3 create a new CMS block in your backend with the ID "footer_block_extra" ... and add your content.

Lacreshalacrimal answered 30/9, 2014 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.