nexcess / magento-turpentine

A Varnish extension for Magento.
GNU General Public License v2.0
520 stars 252 forks source link

dynamic cms block in phtml file #534

Closed jg-development closed 10 years ago

jg-development commented 10 years ago

Hi,

first of all. I think Turpentine is a great extension and I think I will use it where I can ;-). It speeds up Magento very hard. But I have a little problem, and maybe somebody can help me.

Env: Magento 1.7.0.2 PHP 5.5.9 Apache 2.4 Ubuntu 14.04

I have a header.phtml, which I am loading about the xml file:

<block type="page/html_header" name="header" as="header">
                <block type="page/template_links" name="top.links" as="topLinks"/>
                <block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
                <block type="directory/currency" name="currency" as="currency" template="directory/currency/switch.phtml"/>
                <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>
                <block type="page/html_wrapper" name="top.container" as="topContainer" translate="label">
                    <label>Page Header</label>
                    <action method="setElementClass"><value>top-container</value></action>
                </block>
            </block>

I prevent caching it with following option in turpentine:

        <reference name="header">
            <action method="setEsiOptions">
                <params>
                    <method>ajax</method>
                    <access>private</access>
                    <scope>global</scope>
                    <ttl>0</ttl>
                    <dummy_blocks>
                        <root/>
                        <head/>
                    </dummy_blocks>
                </params>
            </action>
        </reference>
            </block>

An Ajax request is noe called and the response is the block. Problem: I have an if/else in the block:

<?php if( $this->getIsHomePage()): ?> <?php echo $this->getLayout()->createBlock("cms/block")->setBlockId("animation")->toHtml() ?> <?php endif; ?>

My wish is, that the block show be seen only on the frontpage, but it always shows up. With this: <?php if( Mage::getSingleton('cms/page')->getIdentifier() == 'home'): ?> Same result.

I think the ajax/esi call has not the identifier (of course) of the homepage. Has somebody a solution for this problem plz?

Greetings tronga

eth8505 commented 10 years ago

I think there are several ways of going about this. The first (and easiest) would be to set a page scope for your header rather than having it as a global block. The obvious disadvantage is that caching will not be very efficient, as the block will be re-rendered for every single page. But since you've set the ttl to 0 anyway, i guess it wouldn't make much of a difference.

The second option I see is actually specifying separate headers for different layout handles. For example you could specify

<default>
your original header implementation
</default

and override it for your homepage

<cms_index_index>
your custom header for the homepage
</cms_index_index>

We actually did that for the footers on one of our projects, since we wanted to have three separate footers for 1) checkout 2) homepage and 3) any other page. To be on the safe side, you would have to implement three completely different header blocks here though. Different names would make sense. The big advantage of this is, that you can actually fully cache the blocks and don't have to fall back to a 0-ttl solution.

I hope this helps a little bit. If it does not, don't hesitate to comment here again.

jg-development commented 10 years ago

Hi, thanks for the quick reply. Because you already noticed, that solution 1 doesnt work, I will try it with the xml structure.

To my disadvantage, I am not really a pro with the magento xml structure.

    <cms_index_index translate="label">
        <reference name="header">
            <block type="core/template" name="page_html_headerhome" template="page/html/headerhome.phtml"/>
        </reference>
    </cms_index_index>

In my cms.xml in I wanted to use this code for the homepage. The code works with a reference "content" or "footer" (phtml is loaded into footer or content), but not with "header". The header is simply ignored.

The default part of the page.xml looks like this:

    <default translate="label" module="page">
        <label>All Pages</label>
        <block type="page/html" name="root" output="toHtml" template="page/2columns-left.phtml">

            <block type="page/html_head" name="head" as="head">
                <action method="addJs"><script>jquery/jquery-1.9.1.min.js</script></action>
                <action method="addJs"><script>prototype/prototype.js</script></action>
                <action method="addJs"><script>lib/ccard.js</script></action>
                <action method="addJs"><script>prototype/validation.js</script></action>
                <action method="addJs"><script>scriptaculous/builder.js</script></action>
                <action method="addJs"><script>scriptaculous/effects.js</script></action>
                <action method="addJs"><script>scriptaculous/dragdrop.js</script></action>
                <action method="addJs"><script>scriptaculous/controls.js</script></action>
                <action method="addJs"><script>scriptaculous/slider.js</script></action>
                <action method="addJs"><script>varien/js.js</script></action>
                <action method="addJs"><script>varien/form.js</script></action>
                <action method="addJs"><script>varien/menu.js</script></action>
                <action method="addJs"><script>mage/translate.js</script></action>
                <action method="addJs"><script>mage/cookies.js</script></action>
                <action method="addJs"><script>jquery/jquery.easing.1.3.js</script></action>
                <action method="addJs"><script>jquery/jquery.ui.totop.min.js</script></action>
                <action method="addJs"><script>jquery/jquery-ui.min.js</script></action>

                <block type="page/js_cookie" name="js_cookies" template="page/js/cookie.phtml"/>

                <action method="addCss"><stylesheet>css/styles.css</stylesheet></action>
                <action method="addItem"><type>skin_css</type><name>css/styles-ie.css</name><params/><if>lt IE 8</if></action>
                <action method="addCss"><stylesheet>css/widgets.css</stylesheet></action>
                <action method="addCss"><stylesheet>css/print.css</stylesheet><params>media="print"</params></action>

                <action method="addItem"><type>js</type><name>lib/ds-sleight.js</name><params/><if>lt IE 7</if></action>
                <action method="addItem"><type>skin_js</type><name>js/ie6.js</name><params/><if>lt IE 7</if></action>
            </block>

            <block type="core/text_list" name="after_body_start" as="after_body_start" translate="label">
                <label>Page Top</label>
            </block>

            <block type="page/html_notices" name="global_notices" as="global_notices" template="page/html/notices.phtml" />

            <block type="page/html_header" name="header" as="header">
                <block type="page/template_links" name="top.links" as="topLinks"/>
                <block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
                <block type="directory/currency" name="currency" as="currency" template="directory/currency/switch.phtml"/>
                <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>
                <block type="page/html_wrapper" name="top.container" as="topContainer" translate="label">
                    <label>Page Header</label>
                    <action method="setElementClass"><value>top-container</value></action>
                </block>
            </block>

            <block type="page/html_breadcrumbs" name="breadcrumbs" as="breadcrumbs"/>

            <block type="core/text_list" name="left" as="left" translate="label">
                <label>Left Column</label>
            </block>

            <block type="core/messages" name="global_messages" as="global_messages"/>
            <block type="core/messages" name="messages" as="messages"/>

            <block type="core/text_list" name="content" as="content" translate="label">
                <label>Main Content Area</label>
            </block>

            <block type="core/text_list" name="right" as="right" translate="label">
                <label>Right Column</label>
            </block>

            <block type="page/html_footer" name="footer" as="footer" template="page/html/footer.phtml">
                <block type="page/html_wrapper" name="bottom.container" as="bottomContainer" translate="label">
                    <label>Page Footer</label>
                    <action method="setElementClass"><value>bottom-container</value></action>
                </block>
                <block type="page/switch" name="store_switcher" as="store_switcher" template="page/switch/stores.phtml"/>
                <block type="page/template_links" name="footer_links" as="footer_links" template="page/template/links.phtml"/>
            </block>

            <block type="core/text_list" name="before_body_end" as="before_body_end" translate="label">
                <label>Page Bottom</label>
            </block>
        </block>

        <block type="core/profiler" output="toHtml" name="core_profiler"/>
    </default>

I am trying now for about an hour, but I cannot get Magento to load the headerhome insted of the header .... strange in my eyes.

Do you have any ideas ... or maybe you can send me the xml structure of your footer layout?

Thanks

tronga

jg-development commented 10 years ago

OK I got it now ;-)

    <cms_index_index translate="label">
        <label>CMS Home Page</label>
        <reference name="header">
            <block type="page/header" name="header">
                <action method="setTemplate"><template>page/html/headerhome.phtml</template></action>
            </block>
        </reference>
    </cms_index_index>

this does work now .... i think i close this ticket.... thank you for your help