Dynamic or lazy blocks in a CMS page with Lesti_FPC

Simon Sprankel contacted me a few days ago. He is using Lesti_FPC for a customer and he had a block in a few CMS pages. This block has to be dynamic or lazy. I have answered this tricky question a few times via email, but together we have found a good solution that I would like to present in this post.

A Magento template

I will create a short example to explain a little bit more detailed. Let us assume we have the following template.

<p>
    <?php echo __(
        "Hello %s %s, the current time is %s",
        $this->getFirstName(),
        $this->getLastName(),
        date("H:i:s")
    ); ?>
</p>

Embed the template in a CMS page

We can embed the template as core/template in a CMS with the following code in the Content of the CMS page. I know it makes not that much sense to inject a first and last name into a template, but I needed just a small example with more than one parameter and something dynamic like the time.

{{block type="core/template"
    template="example/timemessage.phtml"
    first_name="Bruce"
    last_name="Wayne"}}

Hole punching the block

The current example template will be cached by FPC and the time is not updating. We have to change three things to get the block dynamic.

Name in Layout

Let us add name_in_layout to the block directive. The value has to be the same as name in the next step.

{{block type="core/template"
    template="example/timemessage.phtml"
    first_name="Bruce"
    last_name="Wayne"
    name_in_layout="timemessage"}}

Layout Update XML

And we will add the block to Layout Update XML in the Design tab of the CMS page.

<block type="core/template" template="example/timemessage.phtml" name="timemessage">
    <action method="setFirstName"><first_name>Bruce</first_name></action>
    <action method="setLastName"><last_name>Wayne</last_name></action>
</block>

Dynamic or lazy

We just have to add the name of the block in layout to the dynamic or lazy blocks in the configurations of the FPC. In the current case that would be timemessage.

After cleaning all caches we should see <p>Hello Bruce Wayne, the current time is 18:16:08</p> in our CMS page with updated time but served from FPC.

Next Previous