How to add custom dynamic block

After first rendering of page, extension check allow/disallow rules, cacheable actions and save this page (with all blocks) to cache. Then extension will return cached page without new rendering.

For update certain blocks extension provided an ability to define dynamic blocks.

Dynamic block - cached block with information which updated based on predefined conditions. If text inside parameter depends is empty (<depends></depends>) block will be excluded from cache.

Native dynamic blocks:

  • page/html_header
  • checkout/cart_sidebar
  • page/html_wecome
  • reports/product_viewed
  • catalog/product_compare_sidebar
  • wishlist/customer_sidebar
  • page/template_links
  • page/html_notices
  • page/html_cookieNotice
  • cookienotice/notice (OptimiseWeb)

All dynamic blocks defined in files:

  • app/code/local/Mirasvit/Fpc/etc/cache.xml - contains native dynamic blocks
  • app/code/local/Mirasvit/Fpc/etc/custom.xml - contains custom (for current store) dynamic blocks
  • app/code/local/Mirasvit/Fpc/etc/cachelogged.xml - contains custom (for logged in users) dynamic blocks

Structure of custom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <containers>
        <zblock>
            <block>zblocks/block</block>
            <container>Mirasvit_Fpc_Model_Container_Base</container>
            <depends></depends>
            <in_app>0</in_app>
        </zblock>
    </containers>
</config>

where:

  • block - type of the block
  • container - the model, which implements logic of dynamic block cache

    In most cases, you can use Mirasvit_Fpc_Model_Container_Base

  • depends - the set of conditions for update block cache (comma seperator)

    • customer - current customer
    • customer_group - current customer group
    • cart - shopping cart
    • compare - compare list
    • wishlist - wishlist
    • product - current product
    • category - current category
    • store - current store
    • currency - current currency
    • locale - current locale
    • rotator - rotate block (save 5 different variants of the block and then return its random from cache)
    • empty value - constantly update block (always fresh)
  • in_app - logic for block update

    • 0 - allow block update without rendering whole page
    • 1 - update block only with rendering whole page
  • in_register - Optionally you can use in register in_register ( <in_register>current_category,current_product,product</in_register> ).
    • current_category - If excluded block use Mage::registry('current_category')
    • current_product - If excluded block use Mage::registry('current_product')
    • product - If excluded block use Mage::registry('product').

If you exclude block core/template, core/text_list or other block which exist with different names use also parameter name to show which block should be excluded.

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <containers>
      <zblock>
          <block>zblocks/block</block>
          <name>blockname</name>
          <container>Mirasvit_Fpc_Model_Container_Base</container>
          <depends></depends>
          <in_app>0</in_app>
      </zblock>
  </containers>
</config>

I have an error after block excluding. How can I fix it?

If you have excluded blocks that use Mage::registry('current_category'), Mage::registry('current_product') or Mage::registry('product') you can get an error like this "Fatal error: Uncaught Error: Call to a member function getMetaTitle() on null in /app/code/core/Mage/Catalog/Block/Product/View.php:56" or similar. To fix the error you'll need to add in_register parameter.

  • current_category - if excluded block use Mage::registry('current_category')

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
      <containers>
          <zblock>
              <block>zblocks/block</block>
              <name>blockname</name>
              <container>Mirasvit_Fpc_Model_Container_Base</container>
              <depends></depends>
              <in_register>current_category</in_register>
              <in_app>0</in_app>
          </zblock>
      </containers>
    </config>
  • current_product - if excluded block use Mage::registry('current_product')

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
      <containers>
          <zblock>
              <block>zblocks/block</block>
              <name>blockname</name>
              <container>Mirasvit_Fpc_Model_Container_Base</container>
              <depends></depends>
              <in_register>current_product</in_register>
              <in_app>0</in_app>
          </zblock>
      </containers>
    </config>
  • product - if excluded block use Mage::registry('product')

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
      <containers>
          <zblock>
              <block>zblocks/block</block>
              <name>blockname</name>
              <container>Mirasvit_Fpc_Model_Container_Base</container>
              <depends></depends>
              <in_register>product</in_register>
              <in_app>0</in_app>
          </zblock>
      </containers>
    </config>

    If you don't know which parameter add in in_register you can use <in_register>current_category,current_product,product</in_register>.

Example of adding custom dynamic block

To exclude block from cache, this block must be declared in the xml file at the current theme layout folder.
If the block is not declared at layout files, you need To create a new block.

  1. Go to System > Configuration > Advanced > Developer and open Debug block.
    Select the required store view.
    Set Yes at the option Template Path Hints to show all template paths on your frontend.

  2. Select a page template. For example, we need to exclude the menu block from fpc cache.
    To exclude this block, go to your frontend store page and check the template path near required block:
    image
    The current example path to the menu block: frontend/rwd/default/template/page/html/topmenu.phtml

  3. Using global search find the layout (in your theme) where the template is applied.
    Copy the phrase which starts after /template/ path: page/html/topmenu.phtml.
    Search path page/html/topmenu.phtml to find the file on your server, where the template is set up.
    For this purpose, you can use sublime-text. Search results should be:

    Found File: /magento_root/app/design/frontend/base/default/layout/page.xml

                ----------------------------------------------------------------------------------------------
                block type="core/text_list" name="top.menu" as="topMenu" translate="label">
                    <label>Navigation Bar</label>
                    <block type="page/html_topmenu" name="catalog.topnav" template="page/html/topmenu.phtml"/>
                </block>
                ----------------------------------------------------------------------------------------------

    From the found file we need to get the block details:

                    <block type="page/html_topmenu" name="catalog.topnav" template="page/html/topmenu.phtml"/>
  4. Add current block details to custom.xml file.
    Create a new file custom.xml at /magento_root/app/code/local/Mirasvit/Fpc/etc/.
    Add next content to the created file:

                    <?xml version="1.0" encoding="UTF-8"?>
                    <config>
                        <containers>
                            <page_html_topmenu>
                                <block>page/html_topmenu</block>
                                <name>catalog.topnav</name>
                                <container>Mirasvit_Fpc_Model_Container_Base</container>
                                <depends></depends>
                                <in_app>false</in_app>
                            </page_html_topmenu>
                        </containers>
                    </config>

    If the file custom.xml already exists, you need to add new block details between <containers> and </containers> tags.

  5. Refresh cache in "Cache Storage Management".
  6. Check if block is excluded.

Example of adding Static Block as dynamic block (from version 1.0.14)

Variant 1

1.) Add unique tags to the begin and to the end of static block
image

2.) You can exclude the block from FPC cache if add the following code to the file /app/code/local/Mirasvit/Fpc/etc/custom.xml:

                    <?xml version="1.0" encoding="UTF-8"?>
                    <config>
                        <containers>
                             <home_decor_block_exclude>
                                <block>cms/block</block>
                                <container>Mirasvit_Fpc_Model_Container_Cmsblock</container>
                                <block_id>home-decor</block_id>
                                <replacer_tag_begin><![CDATA[<!-- home_decor_block_exclude_begin -->]]></replacer_tag_begin>
                                <replacer_tag_end><![CDATA[<!-- home_decor_block_exclude_end -->]]></replacer_tag_end>
                                <depends></depends>
                            </home_decor_block_exclude>
                        </containers>
                    </config>

where 'block_id' it is Static Block Identifier.
3.) Refresh cache in "Cache Storage Management".
4.) Check if block is excluded.

Variant 2

If you need exclude block like this {{block type="core/temoplate" name="block" template="page/html/myblock.phtml"}}
1.) Add unique tags to the begin and to the end of static block image
2.) You can exclude the block from FPC cache if add the following code to the file /app/code/local/Mirasvit/Fpc/etc/custom.xml:

                    <?xml version="1.0" encoding="UTF-8"?>
                    <config>
                        <containers>
                             <core_template_block>
                                <block>core/template</block>
                                <container>Mirasvit_Fpc_Model_Container_Phtmlblock</container>
                                <template>page/html/myblock.phtml</template>
                                <replacer_tag_begin><![CDATA[<!-- block_exclude_begin -->]]></replacer_tag_begin>
                                <replacer_tag_end><![CDATA[<!-- block_exclude_end -->]]></replacer_tag_end>
                                <depends></depends>
                            </core_template_block>
                        </containers>
                    </config>

3.) Refresh cache in "Cache Storage Management".
4.) Check if block is excluded.

Variant 3 (from version 1.0.75)

If you need exclude block like this {{block type="catalog/product_list" name="product_list_t" category_id="5" column_count="3" count="6" limit="4" mode="grid" toolbar_block_name="product_list_toolbar" template="catalog/product/list.phtml"}}
1.) Add unique tags to the begin and to the end of static block
image
2.) You can exclude the block from FPC cache if add the following code to the file /app/code/local/Mirasvit/Fpc/etc/custom.xml:

                    <?xml version="1.0" encoding="UTF-8"?>
                    <config>
                        <containers>
                            <catalog_product_list>
                                <block>catalog/product_list</block>
                                <name>product_list_t</name>
                                <container>Mirasvit_Fpc_Model_Container_Phtmlblock</container>
                                <template>catalog/product/list.phtml</template>
                                <replacer_tag_begin><![CDATA[<!-- block_exclude_product_list_t_begin -->]]></replacer_tag_begin>
                                <replacer_tag_end><![CDATA[<!-- block_exclude_product_list_t_end -->]]></replacer_tag_end>
                                <additional_data>
                                    <category_id>5</category_id>
                                    <column_count>3</column_count>
                                    <count>6</count>
                                    <limit>4</limit>
                                    <mode>grid</mode>
                                    <toolbar_block_name>product_list_toolbar</toolbar_block_name>
                                </additional_data>
                                <depends></depends>
                            </catalog_product_list>
                        </containers>
                    </config>

3.) Refresh cache in "Cache Storage Management".
4.) Check if block is excluded.

Example of adding block as dynamic block if block added in template (from version 1.0.11)

For example, we have following block

<?php echo Mage::app()->getLayout()->createBlock('core/template')->setTemplate('page/html/infoblock.phtml')->toHtml(); ?>  

in template.
1.) Add unique tags to the begin and to the end of the block
image

2.) You can exclude the block from FPC cache if add the following code to the file /app/code/local/Mirasvit/Fpc/etc/custom.xml:

                    <?xml version="1.0" encoding="UTF-8"?>
                    <config>
                        <containers>
                             <core_template_infoblock>
                                <block>core/template</block>
                                <container>Mirasvit_Fpc_Model_Container_Phtmlblock</container>
                                <template>page/html/infoblock.phtml</template>
                                <replacer_tag_begin><![CDATA[<!-- infoblock_exclude_begin -->]]></replacer_tag_begin>
                                <replacer_tag_end><![CDATA[<!-- infoblock_exclude_end -->]]></replacer_tag_end>
                                <depends></depends>
                            </core_template_infoblock>
                        </containers>
                    </config>

3.) Refresh cache in "Cache Storage Management".
4.) Check if block is excluded.


How to create a new block from particular code

Follow these steps to create a new block for the specific code.

  • For example, you want to exclude block <div class="header-static-block"> from the file /app/design/frontend/[your_package]/[your_theme]/template/page/html/header.phtml:

    image

  • 1) At the same folder you need to create a new file, for example ipdetail.phtml: /app/design/frontend/[your_package]/[your_theme]/template/page/html/ipdetail.phtml,
    where you need to copy this particular code from header.phtml file:

    image

  • 2) Create copy of the original header.phtml file, for example: header.phtml_ORIG.

  • 3) Delete or comment out the the copied code (block header-static-block) at the current header.phtml file:

    image

  • 4) Add this code to the file /app/design/frontend/[your_package]/[your_theme]/layout/page.xml inside of header block:

    image

                <block type="core/text_list" name="ip_detail_text_list_block" as="ip_detail_text_list_block" translate="label">
                    <label>ip detail</label>
                </block>

where:
name="ip_detail_text_list_block" - any reference name for this block,
as="ip_detail_text_list_block" - alias for this block (the same as name).

If you don't have page.xml file at the folder /app/design/frontend/[your_package]/[your_theme]/layout/, you need to copy it from /app/design/frontend/base/default/layout.

  • 5) Add this code to the file /app/design/frontend/[your_package]/[your_theme]/layout/local.xml:

                <default>
                    <reference name="ip_detail_text_list_block">
                    <block type="core/template" name="ip_detail_block" template="page/html/ipdetail.phtml" />
                    </reference>
                </default>

where:
reference name="ip_detail_text_list_block" - reference block name, which we declared previously in the page.xml file,
name="ip_detail_block" - any name you want for this block,
template="page/html/ipdetail.phtml" - path to the new created file ipdetail.phtml. Sets only path after /template/ folder.

If you don't have local.xml file at this layout folder, you need to create this file and add these lines:

                <?xml version="1.0" encoding="UTF-8" ?>
                <layout>
                    <default>
                        <reference name="ip_detail_text_list_block">
                            <block type="core/template" name="ip_detail_block" template="page/html/ipdetail.phtml" />
                        </reference>
                    </default>
                </layout>

  • 6) Add next lines to the file header.phtml:

    image

<?php echo $this->getChildHtml('ip_detail_text_list_block') ?>

After cache is flushed, you will see this block in frontend.

Finally, you can exclude the block from FPC cache if add the following code to the file /app/code/local/Mirasvit/Fpc/etc/custom.xml:

                <?xml version="1.0" encoding="UTF-8"?>
                <config>
                    <containers>
                        <core_template_ip_detail_block>
                            <block>core/template</block>
                            <name>ip_detail_block</name>
                            <container>Mirasvit_Fpc_Model_Container_Base</container>
                            <depends></depends>
                            <in_app>false</in_app>
                        </core_template_ip_detail_block>
                    </containers>
                </config>


How to disable standard block from cache.xml

For example you need to stop exluding standard block page/html_welcome which you can find in file /app/code/local/Mirasvit/Fpc/etc/cache.xml

    <welcome_message>
        <block>page/html_welcome</block>
        <container>Mirasvit_Fpc_Model_Container_Welcome</container>
        <depends>store,logged_in,customer,customer_group</depends>
        <in_session>true</in_session>
        <in_app>false</in_app>
    </welcome_message>

add the following code in file /app/code/local/Mirasvit/Fpc/etc/custom.xml:

        <?xml version="1.0" encoding="UTF-8"?>
        <config>
            <containers>
                <welcome_message>
                    <disabled>true</disabled>
                </welcome_message>
            </containers>
        </config

and refresh cache to apply results to the fronend.


Example of adding amfinder block as dynamic block

If block added as static block (CMS->Static Blocks):

  1. Go to System > Configuration > Advanced > Developer and open "Debug" tab.
    Select needed store view.
    Set "Template Path Hints" to "Yes" to see all template paths on your frontend.

  2. In frontend you will see something like this:
    image
    In the example above template path is amfinder/horizontal.phtml

  3. Add unique tags at the beginning and to the end of static block
    image

  4. To exclude block from FPC cache you'll need to add following code to the file /app/code/local/Mirasvit/Fpc/etc/custom.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <config>
    <containers>
         <amfinder_form_3>
            <block>amfinder/form</block>
            <container>Mirasvit_Fpc_Model_Container_PhtmlblockAm</container>
            <template>amfinder/horizontal.phtml</template>
            <set_id>3</set_id>
            <replacer_tag_begin><![CDATA[<!-- amfinder_form_3_begin -->]]></replacer_tag_begin>
            <replacer_tag_end><![CDATA[<!-- amfinder_form_3_end -->]]></replacer_tag_end>
            <depends></depends>
        </amfinder_form_3>
    </containers>
    </config>
  5. Refresh cache in "Cache Storage Management".
  6. Check if block is excluded.


How check if block is excluded?

Enable following configuration:
System->Full Page Cache->Settings->Debug->Enable Debug Hints = Yes
System->Full Page Cache->Settings->Debug->Show debug data and logs only for = [your_IP] (Sometimes it may not work if the server always returns 127.0.0.1. In this case, leave this field blank)
In frontend you will see "Cache Miss" for excluded block.
image


Why can't I exclude price block?

You can't exclude price if it is not added as block for product page. If for product page price added as block (you have custom theme) you can exclude it this way.
You can't exclude price for product list, because FPC doesn't know which product should be used.
FPC updates page price if you change prices in admin panel. So in most situations you don't need to exclude price.
If you use specific accounting software you can use this way.
If for some reason you are sure that price need to be exluded you can exclude only parent blocks for price using file custom.xml or cachelogged.xml.
For default magento it will be catalog/product_view and catalog/product_list:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <containers>
        <catalog_product_view>
            <block>catalog/product_view</block>
            <name>product.info</name>
            <container>Mirasvit_Fpc_Model_Container_Base</container>
            <depends></depends>
            <in_register>current_category,current_product,product</in_register>
            <in_app>false</in_app>
        </catalog_product_view>
        <catalog_product_list>
            <block>catalog/product_list</block>
            <name>product_list</name>
            <container>Mirasvit_Fpc_Model_Container_Base</container>
            <depends></depends>
            <in_register>current_category,current_product</in_register>
            <in_app>false</in_app>
        </catalog_product_list>
    </containers>
</config>

But FPC speed will much slower with such excluding.

If you excluded price this way you can set "Use the same cache for all groups = Yes" in System > Full Page Cache > Settings. FPC will start using the same cache for all customer groups.

Full Page Cache