I am working on a custom module template where I need to show some random string only in some part of a page and string should be generate randomly on every time page reload also the rest of the content should be cached content. For this, I have tried following code in layout xml :

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
            <block class="Magento\Framework\View\Element\Template" name="c.parent.block" template="Company_Module::parent.phtml" >
                <block class="Company\Module\Block\Myblock" name="m.child.block" template="Company_Module::child.phtml" />  

parent.phtml file content :

/* Content should be cached */
<?=$this->getChildhtml('m.child.block'); /* Prevent cache content*/ ?>

child.phtml file content :

<?php echo 'Random String : '. rand(); ?>

Above code working fine with my cache enabled store. But I want the above child block should not be cached for this I have passed cacheable="false" attribute with child block. Although this prevent the cache but its also prevent the cache for parent block. Is it possible to prevent the above child block from cache but not its parent block?

Note : I don't want to use any ajax request to generate random string.

1 Answer 1


Try again with:

<?= $this->getChildhtml('m.child.block', false); ?>

With the second parameter is false (default true), Magento 2 will re-render block and don't get it from cache.

  • Thank you @Bill. But it makes both block same (cacheable false) as I mentioned in the question. Commented Jan 20, 2018 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.